share
This time record the mock. Js method used in vuE-CLI for future use.
There is a limit to the number of words on the mining platform. If there is an opportunity to add a long article, the matching rule will be recorded.
The installation
yarn add --dev mockjs
or
npm install mockjs --save-dev
Copy the code
Method of use
Create a mock directory under the SRC directory
The file shown above is index.js
const Mock = require('mockjs');
// Format: Mock. Mock (url, post/get, returned data);
Mock.mock('/user/userInfo'.'get'.require('./json/userInfo'));
Mock.mock('/home/banner'.'get'.require('./json/homeBanner'));
Copy the code
The following uses homeBanner. Json as an example
{
"result": "success".
"data": {
"mtime": "@datetime".//See official documentation for specific applications
"score|1-800": 800.
"rank|1-100": 100.
"stars|1-5": 5.
"nickname": "@cname"
},
"msg": ""
}
Copy the code
Introduced the main. Js
import axios from 'axios' // Use axios
Vue.prototype.axios = axios; / / mount axios
require('./mock'); // Introduce mock data, annotate the line when closed
Copy the code
Access interface
this.axios.get('/user/userInfo')
.then(function(res){
console.log(res);
})
.catch(function(err){
console.log(err);
});
Copy the code