标签:response dex userinfo efault vue time process return setup
npm i mockjs --save
if (process.env.NODE_ENV !== ‘production‘) require(‘./mock‘)
当项目启动后,mock会拦截他规则内的http请求
import Mock from ‘mockjs‘ import { getUserInfo } from ‘./response/user‘ let data = Mock.mock(/\/getUser/, ‘get‘, getUserInfo) // 设置响应延时 // Mock.setup({ // timeout: 5000 // }) export default data
import Mock from ‘mockjs‘ const Random = Mock.Random export const getUserInfo = (options) => { let userInfo = [] for (let i = 0; i < 10; i++) { let template = { ‘name‘: Random.cname(), ‘age‘: Random.natural(22, 40), ‘date‘: Random.date(‘yyyy-MM-dd‘), ‘address‘: Random.county(true) } userInfo.push(template) } // let i = 3 // let arr = [] // while (i--) { // arr.push(template) // } // return Mock.mock(userInfo) return userInfo }
mock基本配置完成
import axios from ‘./index‘ export const getUserInfo = () => { return axios.request({ url: ‘/getUser‘, method: ‘get‘ }) }
vue中获取数据
getUserInfo().then(res => {
// console.log(res.data)
this.tableData = res.data
})
参考:http://www.likecs.com/show-52362.html
https://segmentfault.com/a/1190000015682126
标签:response dex userinfo efault vue time process return setup
原文地址:https://www.cnblogs.com/jvziking/p/11474555.html