鍍金池/ 問答/HTML/ 為什么axios.interceptors執(zhí)行了兩次。

為什么axios.interceptors執(zhí)行了兩次。

為什么axios.interceptors執(zhí)行了兩次

 
 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');

打印了兩行“----”。其他地方?jīng)]有設置axios.interceptors

回答
編輯回答
瘋浪

我也碰到了,初步發(fā)現(xiàn),是因為重復注冊了這個攔截器導致。

2018年2月17日 02:44