react-mockjs


2020-01-17

react-mockjs 使用

最近參加了公司的一個新的項目,前后端同時開發,這時后端提供不了前端接口,那么就要靠咱們前端自己mock數據啦。

用到mock 數據的工具是 mockjs ,就是這個。

 

 

 這里是github地址:https://github.com/nuysoft/Mock/wiki/Getting-Started

 

# 安裝
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 有很多方便之處,它的規范和用法官網寫的很清楚,去官網看哦

1、安裝:https://github.com/nuysoft/Mock/wiki/Getting-Started

2、規范:https://github.com/nuysoft/Mock/wiki/Syntax-Specification

3、占位符@XXX規范:https://github.com/nuysoft/Mock/wiki/Mock.Random

4、炒雞全面的示例:http://mockjs.com/examples.html

5、一個很詳細的解釋:https://www.jianshu.com/p/9dbcfbe6130f


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM