Mock.js is introduced in vue

    npm install mockjs --save-dev
Copy the code

Create a Mock folder and create the index.js file

The index.js file is as follows

import Mock from 'mockjs'Mock. Setup ({timeout:'200-600'// Can be an integer or a '-' string});letconfigArray = []; // Walk through all mock files using webpack's require.context() const files = require.context()'. '.true, /\.js$/);
files.keys().forEach((key) => {
    if(key === './index.js') return; configArray = configArray.concat(files(key).default); }); // Register all mock services configarray. forEach((item) => {for(let [path, target] of Object.entries(item)){
        let protocol = path.split('|');
        Mock.mock(new RegExp(A '^'+ protocol[1]), protocol[0],target); }})Copy the code

The main js file

import Vue from "vue";
import App from "./App";
import router from "./router";
import store from "./vuex/store"; // import axios from'axios';

if(process.env.NODE_ENV ! = ='production') require('./mock')


Vue.config.productionTip = false;
Vue.prototype.$axios = axios;

/* eslint-disable no-new */
new Vue({
  el: "#app", router, store,// equivalent to store:store, after registration, child components can use this.$storeAccess components: {App}, template:"<App/>"
});

Copy the code

Next you can create a data file, such as demolist.js

let demoList = [
  {
    id: 1,
    text: 1 "test"}];export default {
  "get|/parameter/query": option => {
    return {
      status: 200,
      message: "success", data: demoList }; }};Copy the code

And then you can quote it. Such as:

    getMock() {/ / mock call this.$axios({
          url:'/parameter/query',
          method: "get",
        }).then(res =>{
        console.log(res.data);
      });
    },
Copy the code