How does Vue2.5 read data through json file
This article mainly introduces Vue2.5 how to read data through the json file related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this Vue2.5 how to read data through the json file article will have a harvest, let's take a look at it.
1. Preparatory work
1.1 webpack.dev.conf.js
Add the following code under const portfinder = require ('portfinder')
Const express = require ('express') const app = express () var appData = require ('.. / data.json') / / the path to load the local data file var leftMenu = appData.leftMenu / / get the corresponding local data var data name = appData. Option var apiRoutes = express.Router () app.use ('/ api', apiRoutes)
Find devServer and add some code to it
Before (app) {app.get ('/ api/leftmenu', (req, res) = > {res.json ({errno: 0, data: leftMenu}) / / API returns json data. The above configured data seller is called after the data request is assigned}), app.get ('/ api/ data', (req, res) = > {res.json ({errno: 0, data: data (corresponding to the above data name})}
two。 In the components used
Created () {this.$http.get ('api/leftmenu'). Then ((response) = > {console.log (response) this.leftMenu = response.body.data / / data location})} data () {return {leftMenu: [];}} this is the end of the article on "how Vue2.5 reads data through json files". Thank you for reading! I believe you all have a certain understanding of "how Vue2.5 reads data through json files". If you want to learn more, you are welcome to follow the industry information channel.