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
dva或umi框架使用mockjs模拟数据
2019-11-14 07:46:30
127
0
0
admin
## [官网](http://mockjs.com/examples.html) ## 第一步:在框架下的mock文件夹下新建users.js(对应users路由下的页面) const Mock = require('mockjs');//导入mock.js模块 const config=require("../src/utils/config"); const {apiPrefix}=config; const userData=Mock.mock({ 'data|95':[{ 'key|+1':1, 'name':'Edward King', 'age':32, 'address':'London, Park Lane no1' }] }) module.exports={ //post 请求 /api/v1/users/是拦截地址 方法内部接受request response对象 [`GET ${apiPrefix}/users`](req,res){ res.json(userData) } } ## 第二步:在.roadhogrc.mock.js(dva)或者.umirc.mock.js(umi)中导入mock const mock = {} require('fs').readdirSync(require('path').join(__dirname + '/mock')).forEach(function(file) { Object.assign(mock, require('./mock/' + file)) }) module.exports = mock ## 第三步:需要数据的地方用axios请求 import axios from "axios" axios.get(`GET ${apiPrefix}/users`, {}) .then(res=> { console.log(res) }) ## <span color="red" data-mce-style="color: red;" style="color: red;">注意</span> 1. 如果报404错误的话, 检查是否开启了本地代理, 如: ``` "proxy": { "/api/v1": {} } ``` 则不能使用`/api/v1`开头的路径
Pre:
preflight request(预检请求)
Next:
在html中js如何给字符串中加换行符
0
likes
127
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.