安装
npm install mockjs
全局引入
- main.js
import "@/utils/mock";
配置
- src/utils/mock.js
let Mock = require('mockjs');
// 例1
Mock.mock('/api-infos/user/get-info', function () {
return Mock.mock({
success: true,
msg: '',
obj: {
"account_name": "@name",
"gender|1": ["male", "female"],
"birthday": Mock.Random.datetime('yyyy-MM-dd'),
"address": "a b c d"
}
})
});
// 例2
Mock.mock('/api-infos/user/message', function (req) {
let pgSize = JSON.parse(req.body).pgSize;
let res = {
success: true,
msg: '',
obj: []
}
for (let i = 0; i < pgSize; i++) {
res.obj.push(Mock.mock({
"subject|2-10": "aaaaa",
"id|1-100": 100,
"time": Mock.Random.datetime('yyyy-MM-dd HH:mm'),
"type|1": ['order', 'system'],
"tip": '您有一条新的提货消息,速来提取>>',
"status|1": ['seen', 'unseen']
}))
}
return res;
});
使用
配置完成之后, 就可以正常使用了
请求接口就是 mock 中自己设置的接口