vue项目引入 本地json模拟后台数据(参考https://www.cnblogs.com/wymbk/p/8275767.html)
步骤:首先将json文件放在与index.html同级的根目录。
再在webpack.dev.conf.js中 const portfinder = require("portfinder");之后添加
var appData = require('../route.json')//加载本地数据文件
var componentList = appData.data//获取对应的本地数据
在devServer里面添加
before(app) {
app.get('/api/componentList', (req, res) => {
res.json({
data: componentList
})//接口返回json数据,上面配置的数据seller就赋值给data请求后调用
})
}
在页面方法中使用直接调用
getRoute() {
axios.get("/api/componentList").then(res=>{
console.log(res)
}).catch(res=>{
console.log(res)
});
},