1、新建 mock/mock.js
新建 mock/mock.js
const Mock = require('mockjs');
Mock.mock('/news/list', {
'items|20': [{
id: '@id',
name: '@name',
title: '@sentence(10, 20)',
'status|1': ['published', 'draft', 'deleted'],
author: '@cname', display_time: '@datetime',
pageviews: '@integer(300, 5000)'
}]
})
let addData = []
Mock.mock('/mock/add', ops => {
addData.push(ops)
})
Mock.mock('/mock/getAdd', addData)
// module.exports = [{
// url: '/news/list',
// type: 'get',
// response: config => {
// const items = data.items
// return {
// code: 20000,
// data: {
// total: items.length,
// items: items
// }
// }
// }
// }]
2、main.js中引入
require('./mock/index.js')
3、页面使用
<div>
<input type="text" v-model="postData">
<button type="button" @click="submit">提交</button>
<button type="button" @click="getData">获取</button>
</div>
methods: {
submit () {
axios.post('/mock/add', { // 添加数据的接口,数据为一个对象,有个title属性
postData: this.postData
})
},
getData () {
axios.get('/mock/getAdd').then(res => { // 获取数据
console.log(res.data)
})
}
},