react-mockjs

2020-01-17

react-mockjs 使用

最近参加了公司的一个新的项目,先后端同时开发,这时后端提供不了前端接口,那么就要靠我们前端本身mock数据啦。html

用到mock 数据的工具是 mockjs ,就是这个。前端

 

 

 这里是github地址:https://github.com/nuysoft/Mock/wiki/Getting-Startedreact

 

# 安装
npm install mockjs

 

# 导入 mockjs
import mockjs from 'mockjs';

 

const proxy = {'GET /api/getuser': (req, res) => res.send(mockjs.mock({ // /api/getuser 是我须要访问的接口名称 "code": 0, "content|1": [{ // content|1 content 是我最终想要拿到的数据  1是指生成一个content  
       "id|+1": 1,
"name": "@cname",
"job|1": ["前端工程师", "前端助理", "前端专家", "运维专家", "后端工程师"],
"jobNum|1": [1, 12, 34, 56, 23, 45, 56, 34, 56, 77, 4, 321, 443, 654, 231],
"date": "@date",
"jobHour": "@integer",
  }],
 success:
true,
message:
'123',
})) }
export default delay(proxy, 1000);
 

 

mockjs 有不少方便之处,它的规范和用法官网写的很清楚,去官网看哦git

一、安装:https://github.com/nuysoft/Mock/wiki/Getting-Startedgithub

二、规范:https://github.com/nuysoft/Mock/wiki/Syntax-Specificationnpm

三、占位符@XXX规范:https://github.com/nuysoft/Mock/wiki/Mock.Random后端

四、炒鸡全面的示例:http://mockjs.com/examples.htmlapi

五、一个很详细的解释:https://www.jianshu.com/p/9dbcfbe6130f前端工程师