vue - mock 模拟数据
mock/index.js
import Mock from 'mockjs' import user from './data/User' import shopping from "./data/Shopping"; //设置延时 Mock.setup({ timeout: 400 }) const mock = { install: function (Vue) { Vue.use(Mock.mock("/home/data",'get',user)) Vue.use(Mock.mock("/home/shopping",'get',shopping)) } } export default mock
mock/data/User.js
// 引入Mock import Mock from 'mockjs' // 定义数据 const users = Mock.mock({ 'data|4': [{ date: '@date', name: '@cname', address: '@city(true)' }] }) function list (res) { // res是一个请求对象,包含: url, type, body return { code: 200, data: users.data, message: '请求成功' } } export default list
mock/data/Shopping.js
// 引入Mock import Mock from 'mockjs' // 定义数据 const shoppinData = Mock.mock({ 'data|6': [{ "balance|2000-20000": 1, 'info|+1': [ '今日待支付', '昨日订单', '本周订单', '本月待支付' ] }] }) function list (res) { // res是一个请求对象,包含: url, type, body return { code: 200, data: shoppinData.data, message: '请求成功' } } export default list
main.js
//mock import mock from './mock/index' Vue.use(mock)
官网:http://mockjs.com/