cli3 推崇自动化!和 cli2有很大区别。vue
1.在根目录下建立一个mock文件夹,用来存放模拟数据json文件;ios
二、在根目录中找到vue.config.js这个文件,修改成下面的配置:json
const appData = require('./mock/data.json')
// 正常写1个常量便可,下面3个常量能够忽略
const seller = appData.seller
const goods = appData.goods
const ratings = appData.ratings
module.exports = {
devServer:{
port:8080,
// 3个mock
before(app){
app.get("/api/seller",(req,res)=>{
res.json({
errno:0,
data:seller
})
})
app.get("/api/goods",(req,res)=>{
res.json({
errno:0,
data:goods
})
}),
app.get("/api/ratings",(req,res)=>{
res.json({
errno:0,
data:ratings
})
})
}
}
};
复制代码
测试mock是否建立成功 在vue组件里axios
a(){
axios.get("/api/seller").then(res => {
console.log(res)
})
},
复制代码
搞定!api