报错提示
`GET http://localhost:8080/api/index.json 404 (Not Found)
**Uncaught (in promise) Error: Request failed with status code 404
at createError (createError.js?2d83:16)
at settle (settle.js?467f:17)
at XMLHttpRequest.handleLoad (xhr.js?b50d:61)**`
vue.config.js
var path = require("path");
function resolve(dir) {
console.log(__dirname)
return path.join(__dirname, dir)
}
module.exports = {
chainWebpack: config => {
config.resolve.alias.set("css", resolve("src/assets/css")); // key,value自行定义,比如.set('@@', resolve('src/components'))
},
devServer: {
proxy: {
"/api": {
target: "http://localhost:8080",
ws: true,
changeOrigin: true,
pathRewrite: {
"^/api": "/public/mock"
}
}
}
}
};
Home.vue
<template>
<div>
<home-header></home-header>
<home-swiper></home-swiper>
<home-icons></home-icons>
<home-recomment></home-recomment>
<home-weekend></home-weekend>
</div>
</template>
<script>
import HomeHeader from "./components/Header";
import HomeSwiper from "./components/Swiper";
import HomeIcons from "./components/Icons";
import HomeRecomment from "./components/Recomment";
import HomeWeekend from "./components/Weekend";
import axios from "axios";
export default {
name: "Home",
// data: function() {},
components: {
HomeHeader,
HomeSwiper,
HomeIcons,
HomeRecomment,
HomeWeekend
},
mounted() {
this.getHomeInfo();
},
methods: {
getHomeInfo() {
// axios.get("/public/mock/index.json").then(this.getHomeInfoSucc());
axios.get("/api/index.json").then(this.getHomeInfoSucc());
},
getHomeInfoSucc(res){
console.log(res);
}
}
};
</script>
<style lang="stylus" scoped></style>
###
1.既然mock的数据放在了public中,那么完全没必要使用代理,都是8080端口,可以直接访问。
2.axios.then(),这里的then, 里面放的是一个函数定义: axios.then(this.getHomeInfoSucc),而不是放一个函数调用: then(this.getHomeInfoSucc())。
###你的写法不对啊,如果是地址栏输入 http://localhost:8080/js/index.json
可以获取数据,那么你的路径不应该是 /public/mock
啊
pathRewrite: {
"^/api": "/js"
}