问答

vue.config.js proxy代理 报错

作者:admin 2021-09-23 我要评论

报错提示 `GET http://localhost :8080/api/index.json 404 (Not Found) **Uncaught (in promise) Error: Request failed with status code 404 at createError ...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

报错提示

`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"
}

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • vue.config.js proxy代理 报错

    vue.config.js proxy代理 报错

  • Eureka集群,配置三个实例只有一台可以

    Eureka集群,配置三个实例只有一台可以

  • 求助,php使用正则获取链接的首页链接

    求助,php使用正则获取链接的首页链接

  • SPA项目如何做SEO优化?

    SPA项目如何做SEO优化?

腾讯云代理商
海外云服务器