In the process of project development, because we can't interact with the data in the background, we can build a fake data file (such as Data.json) into the project folder so that we can imitate the background data for development. However, how to introduce a local JSON file in a Vue.js project, the following steps are posted. (The project is now packaged by Webpack).
The whole project is packaged by Webpack, and the specific steps are searched online. The specific project structure is as follows:
1: We find bulid>dev-server.js, then open
2: Add this code to the inside.
var app = Express () var appData = require ('.. /data.json '); var seller = Appdata.seller;var goods = Appdata.goods;var Ratings = Appdata.ratings;var Apiroutes = Express.R Outer (); Apiroutes.get ('/seller ', function (req,res) { Res.json ({ errno:0, data:seller });}); Apiroutes.get ('/goods ', function (req,res) { Res.json ({ errno:0, data:goods });}); Apiroutes.get ('/ratings ', function (req,res) { Res.json ({ errno:0, datta:ratings });}); App.use ('/api ', apiroutes);
3: How to use:
You can fill in the browser address bar Http://localhost:8080/api/seller or http://localhost:8080/api/goods or http://localhost:8080/api/ Ratings viewing data
Vue.js Learning Note (ii): How to load a local JSON file