一、安装
npm install mockjs --save
二、文件夹管理
在src下新建mock文件夹,以模拟登陆,获得用户信息,登出请求为例
// index.js
import Mock from 'mockjs'
import loginAPI from './login'
// Mock.setup({
// timeout: '350-600'
// })
// 登录相关
Mock.mock(/\/login\/login/, 'post', loginAPI.loginByUsername) // 拦截了登陆的请求,返回对应数据
Mock.mock(/\/login\/logout/, 'post', loginAPI.logout)
Mock.mock(/\/user\/info\.*/, 'get', loginAPI.getUserInfo)
export default Mock
export default {
loginByUsername: config => {
const { username } = JSON.parse(config.body)
return userMap[username]
},
getUserInfo: config => {
const { token } = param2Obj(config.url)
if (userMap[token]) {
return userMap[token]
} else {
return false
}
},
logout: () => 'success'
}
记得在main.js引入mock
import './mock'