Primer Package
1. NPM Install MOCKJS--save-dev
2. NPM Install Axios--save (Axios use no longer spoken)
Modify Configuration
3. The Dev.env.js under Config folder is modified as follows
' Use strict '= require (' webpack-merge '= require ('./prod.env '= Merge (prodenv, { ' "Development" ', ' true '})
4. The Prod.env.js under Config folder is modified as follows
' Use strict ' Module.exports = { node_env: ' "Production" ', MOCK: ' false '}
5. Make the following configuration in Main.js
Process.env.MOCK && require (' @/mock ')
6. Create the mock directory index.js in the SRC directory with the following code:
Require ('./user ') const Mock = require (' Mockjs ') mock.setup ({ timeout:200})
To create User.js, the code is as follows
Mock.mock ('/api/v1/getuserinfo ', ' get ', { code:200, data: { fullName: ' @CNAME ', email: ' @EMAIL ', user_id:10001213213, username: ' William ', Date: ' @date ', ' array|1-10 ': [ { ' city|+1 ': [ "Beijing", "Shanghai", "Tianjin" ] } ] }, message: ' Success ')
Description:/api/v1/getuserinfo is a custom path, can be placed in a separate file and then introduced, data inside the detailed configuration see: http://mockjs.com/examples.html
Request in any component
this. $http. Get ('/api/v1/getuserinfo '). Then (res =
Console.log (RES)
})
# # # = Note = =
##### 10, mock only the AJAX request inside the project to intercept, do not provide external interface services. An external interface call is not valid.
##### 11, mocks intercept defined URLs, undefined URLs, and mocks do not intercept.
Using Mock.js in VUE-CLI