mock的使用改进,可参考上一篇文章mock的使用
1) mock.js
import Mock from 'mockjs'
const Random = Mock.Random
const dataList = []
for (let i = 0; i < 26; i++) {
const template = {
'id': i,
'Name': Random.name(),
'Address': Random.province()
}
dataList.push(template)
}
import axios from 'axios'
Mock.mock('http://localhost:8080/api/list', 'post', (params) => {
var listQuery = JSON.parse(params.body)
var pageIndex = (listQuery.page - 1) * listQuery.pageSize
var [index, size, total] = [pageIndex, listQuery.pageSize, dataList.length]
var newDataList = dataList.slice(index * size, (index + 1) * size)
return {
'code': '200',
'message': 'success',
'data': {
'listQuery': listQuery,
'tableData': newDataList,
'tableHead': [{ column: 'id', columen_comment: '用户id' }, { column: 'Name', columen_comment: '姓名' }, { column: 'Address', columen_comment: '地址' }],
'total': total
}
}
})
export function list() {
return axios.post('http://localhost:8080/api/list', { page: 1, pageSize: 30 })
}
2) test.vue
<script>
import { list } from './mock'
export default {
mounted() {
this.list()
},
methods: {
list() {
list().then(res => {
console.log(res)
})
}
}
}
</script>