Leanote's Blog
I love Leanote!
Toggle navigation
Leanote's Blog
Home
Chrome
Git
Linux
Windows
Others
工具大全
VsCode
Expo
Html
JavaScript
Npm
Node
Mock
React-Native
React
TypeScript
小程序
插件
正则
Dva
Ant-Design-React
Umi
Vue
Vux
Ant-Design-Vue
Http
Java
flutter
开发小工具
About Me
Archives
Tags
vue使用mock
2021-04-16 09:28:53
8
0
0
admin
### 新建一个文件`mock/index.js` ``` // 首先引入Mock const Mock = require('mockjs') // 设置拦截ajax请求的相应时间 Mock.setup({ timeout: '200-600' }) let configArray = [] // 使用webpack的require.context()遍历所有mock文件 const files = require.context('.', true, /\.js$/) files.keys().forEach((key) => { if (key === './index.js') return configArray = configArray.concat(files(key).default) }) // 注册所有的mock服务 configArray.forEach((item) => { for (let [path, target] of Object.entries(item)) { let protocol = path.split('|') Mock.mock(new RegExp('^' + protocol[1]), protocol[0], target) } }) ``` ### 在`main.js`里引入 ``` import "../mock" //mock文件路径 ``` ### 定义接口 ``` import Mock from "mockjs" import { getUrlParams } from "@/util/util" // 截取get请求传递params时url的参数 export default { 'get|/mock/clouldStoreSKU/page': option => { console.log("option", option) const { page, page_size } = option.body || getUrlParams(option.url) let resData = Mock.mock({ count: 10, page, page_size, unseen_count: 0, [`results|${ page_size }`]: [{ "id|+1": 1, "roleName|1": ["测试角色1", "测试角色2", "测试角色3", "测试角色4", "测试角色5"], "roleCode": "100010", "description": null, "createBy": "jeecg", "createTime": "2019-10-30 19:50:56", "updateBy": null, "updateTime": null }], }) return { status: 200, message: 'success', data: resData }; }, } ``` ### 使用 ``` this.$http.get("/mock/aa/page") ```
Pre:
vue跨标签页共享sessionStorage
Next:
vue函数式组件
0
likes
8
Weibo
Wechat
Tencent Weibo
QQ Zone
RenRen
Submit
Sign in
to leave a comment.
No Leanote account?
Sign up now.
0
comments
More...
Table of content
No Leanote account? Sign up now.