一開始的文件結構是這樣的:
我把 mock 文件夾放在了最外層,實際上這樣的做法是錯誤的。
模擬數據應該被放在 public 文件夾中,這樣就可以正常訪問了。
具體的代碼 mark 如下:
// vue.config.js
module.exports = {
// ...
devServer: {
open: true,
host: 'localhost',
port: 8080,
https: false,
proxy: {
'/api': {
target: 'http://localhost:8080',
pathRewrite: {
'^/api': '/mock'
}
}
}
}
}
// Home.vue
methods: {
getHomeInfo() {
this.$http.get('/api/index.json').then(this.getHomeInfoSucc)
},
getHomeInfoSucc(res) {
console.log(res)
}
},
mounted() {
this.getHomeInfo()
}
訪問本地服務器數據:http://localhost:8080/mock/index.json (不用加public)