为什么axios.interceptors执行了两次。

为什么axios.interceptors执行了两次

 
 new Vue({
    router,
    store,
    created() {
      Vue.prototype.$http = axios;
      this.$http.interceptors.response.use(response => {
        console.log('-----')
                return response.data
            }, error => {
                if (error && error.response && error.response.status == "401") {
          this.$cookies.remove("userName");
          this.$cookies.remove("token");
          //应该清除router路由信息,不然菜单会有问题
          this.$router.options.routes = [];
                    this.$router.push({ path: "/login" });
                }
                return Promise.reject(error)
      });
      if (VueCookies.get("token")) {
        axios.defaults.headers["Authorization"] = VueCookies.get("token");
        store.dispatch("UpdateMenu");
      } else {
        this.$cookies.remove("userName");
        this.$cookies.remove("token");
        //应该清除router路由信息,不然菜单会有问题
        this.$router.options.routes = [];
        this.$router.push({ path: "/login" });
      }
    },
    render: h => h(App)
  }).$mount('#app');

打印了两行“----”。其他地方没有设置axios.interceptors

阅读 6.2k
2 个回答

我也碰到了,初步发现,是因为重复注册了这个拦截器导致。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题