vue-cli的build的文件夹下没有dev-server.js文件,怎么配置mock数据

由于最新版本的vue-cli已经放弃dev-server.js,需在webpack.dev.conf.js配置才行新版本的webpack.dev.conf.js配置如下:
const express require(‘express’)
const app =express()
var appData = require(‘..data.json’)
var seller = appData.seller
var goods = appData.goods
var ratings = appData.ratings
var apiRoutes = express.Router()
app.use(‘/api’, apiRoutes)

找到devServer后,在最后面添加:
before(app) {
app.get(‘/api/appData’,function(req,res){
res.json({
errno:0,
data:appData
})
}),

app.get(‘/api/seller’,function(req,res){
res.json({
errno:0,
data:seller
})
}),

app.get(‘/api/goods’,function(req,res){
res.json({
errno:0,
data:goods
})
}),

app.get(‘/api/ratings’,function(req,res){
res.json({
errno:0,
data:ratings
})
})
}
最后一定要重启才会生效,因为修改了配置文件,重新跑一次npm run dev.打开路径http://localhost:8080/api/appData就能请求到数据打开http://localhost:8080/api/seller 能请求到seller相关数据如图:

文章转载自:vue怎么mock数据

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理