1.首先安装
yarn add mockjs
2.创建mock文件夹,然后创建index.js文件;在文件夹里创建如下代码
// const Mock = require('mockejs');
const Mock = require('mockjs');
function getUserInfo() {
const articles = Mock.mock({
"userInfo|100":[{ //生成|num个如下格式名字的数据
"id|+1":1, //数字从当前数开始后续依次加一
"name":"@cname", //名字为随机中文名字
"ago|18-28":25, //年龄为18-28之间的随机数字
"sex|1":["男","女"], //性别是数组中的一个,随机的
"job|1":["web","UI","python","php"] //工作是数组中的一个
}]
});
return articles;
}
Mock.mock('https://www.test.com/info', 'get', getUserInfo)
3.在入口文件下引入
// 例如
import './mock/index';
4.调用
import React, { Component } from 'react';
import axios from 'axios'
export default class App extends Component {
componentDidMount() {
// 此处调用
axios.get('https://www.test.com/info').then(res => {
console.log(res);
});
}
render() {
return (
<div>
hhahhh
</div>
)
}
}