Detailed two methods of simulated data in Vue-CLI


In Main.js, Vue-Resource module is introduced, Vue.use (VueResource).


1. Use JSON- Server (cannot be requested)

Next to find file under the build directory, in const portfinder = required (‘portfinder’) JSON-Server after

/ * introduced JSON-Server * / Const JSonServer = Require (‘JSON-Server’) / * A server * / const apiserver = jsonserver.create () / * Associate DB.json to Server * / const apirouter = apiserver.router (‘db.json’) const middlewares = jsonserver.defaults () \ ApiServer.use (Apirouter ApiServer.use (MiddleWares) / * Listening Port * / ApiServer.Listen (3000, (REQ, RES) => {Console.log (‘JSON Server IS Running’)}
   Now restart the server's browser address bar Enter the localhost: 3000 to enter the following page, the JSON Server starts successfully 

Find the config file INDEX.JS file under the clamp, find in the DEV configurationTo proxyTable: {} and configure 详解vue-cli中模拟数据的两种方法

‘/ API’: {Changeorigin: True, // Demonstration allows cross-domain Target: “http: // localhost: 3000 “, // Interface domain name Pathrewrite: {‘^ / API’: ‘// Rearwritten path, generally do not make changes}}
   JSON data can be used 

AJAX request is performed through the resource plugin

DATA () {} 详解vue-cli中模拟数据的两种方法 Using hook functions

Created: function () {

this. $ Http.get (' / api / newslist ') .Then (Function (RES) {this.newslist = res. {console.log (err)} {console.log (err)}}} {console.log (err)})}

2. Use Express (which can use POST request)
   New routes file in the project and in it New API.JS, the content is as follows: 
const express = required (‘express’) const router = expression () constraper () ConstApidata = Require (‘../ db.json’) (‘/: Name’, (REQ, RES) => {if (apidata []) {res. json ({‘Error ‘:’ 0 ‘, Data: Apidata [Req.Params.Name]}} Else {Res.}}


Next, find the

file under the build directory, introduced Express in

const portfinder = request (‘portfinder’), as follows:

 const express = Require ('express') const app = expression () const api = required ('../ routes / api.js') app.use ('/ API', API) App.Listen (3000)   
Now found the index.js file under the Config folder, find ProxyTable: {} in the DEV configuration and configure it in it

'/ API': {Changeorigin: True, // Demonstration allows cross-domain Target: "http: // localhost: 3000", // Domain name PathrewRite: {' ^ / API ':' / API '// Use a new path to rewritten, generally not change}}

After reboot, you can request access to data.
The above is the Simulation of the Vue-CLI introduced by Xiaobian. Data’s two methods, I hope to help everyone. If you have any questions, please leave a message, Xiaobian will reply to everyone in time. Thank you very much for your support of Tumi Cloud Website!
© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment