最新的vue沒有dev-server.js檔案,如何進行後臺資料模擬?
阿新 • • 發佈:2019-02-07
參照了別人寫的記錄一下
最新的vue裡dev-server.js被替換成了webpack-dev-conf.js
在模擬後臺資料的時候直接在webpack-dev-conf.js檔案中修改
第一步,在const portfinder = require(‘portfinder’)後新增
//第一步
const express = require('express')
const app = express()//請求server
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()方法
devServer: {
clientLogLevel: 'warning',
historyApiFallback: true,
hot: true,
compress: true,
host: HOST || config.dev.host,
port: PORT || config .dev.port,
open: config.dev.autoOpenBrowser,
overlay: config.dev.errorOverlay
? { warnings: false, errors: true }
: false,
publicPath: config.dev.assetsPublicPath,
proxy: config.dev.proxyTable,
quiet: true, // necessary for FriendlyErrorsPlugin
watchOptions: {
poll: config.dev.poll,
},
//第二步找到devServer,在裡面新增 before(app) {
app.get('/api/seller', (req, res) => {
res.json({
errno: 0,
data: seller
})//介面返回json資料,上面配置的資料seller就賦值給data請求後呼叫
}),
app.get('/api/goods', (req, res) => {
res.json({
errno: 0,
data: goods
})
}),
app.get('/api/ratings', (req, res) => {
res.json({
errno: 0,
data: ratings
})
})
}
}
提供一個json.data資料
{
"seller": {
"name": "粥品香坊(回龍觀)",
"description": "蜂鳥專送",
"deliveryTime": 38,
"score": 4.2,
"serviceScore": 4.1,
"foodScore": 4.3,
"rankRate": 69.2,
"minPrice": 20,
"deliveryPrice": 4,
"ratingCount": 24,
"sellCount": 90,
"bulletin": "粥品香坊其烹飪粥料的祕方源於中國千年古法,在融和現代製作工藝,由世界烹飪大師屈浩先生領銜研發。堅守純天然、0新增的良心品質深得消費者青睞,發展至今成為粥類的引領品牌。是2008年奧運會和2013年園博會指定餐飲服務商。",
"supports": [
{
"type": 0,
"description": "線上支付滿28減5"
},
{
"type": 1,
"description": "VC無限橙果汁全場8折"
},
{
"type": 2,
"description": "單人精彩套餐"
},
{
"type": 3,
"description": "該商家支援發票,請下單寫好發票擡頭"
},
{
"type": 4,
"description": "已加入“外賣保”計劃,食品安全保障"
}
],
"avatar": "http://static.galileo.xiaojukeji.com/static/tms/seller_avatar_256px.jpg",
"pics": [
"http://fuss10.elemecdn.com/8/71/c5cf5715740998d5040dda6e66abfjpeg.jpeg?imageView2/1/w/180/h/180",
"http://fuss10.elemecdn.com/b/6c/75bd250e5ba69868f3b1178afbda3jpeg.jpeg?imageView2/1/w/180/h/180",
"http://fuss10.elemecdn.com/f/96/3d608c5811bc2d902fc9ab9a5baa7jpeg.jpeg?imageView2/1/w/180/h/180",
"http://fuss10.elemecdn.com/6/ad/779f8620ff49f701cd4c58f6448b6jpeg.jpeg?imageView2/1/w/180/h/180"
],
"infos": [
"該商家支援發票,請下單寫好發票擡頭",
"品類:其他菜系,包子粥店",
"北京市昌平區回龍觀西大街龍觀置業大廈底商B座102單元1340",
"營業時間:10:00-20:30"
]
}}
PS:
所有的修改配置都需要重新啟動執行命令的:npm run dev才能生效(很重要,否則無法請求到資料)