vueルーティングブロッカーとリクエストブロッカー

663 ワード

vueブロッキング
  • ルーティングブロック
  • は、ガイド
  • にルーティングする.
    router.beforeEach((to,from,next)=>{
        if(to.path=='/login' || localStorage.getItem('token')){
          next();
        }else{
          alert('     ');
          next('/login');
        }
    })
  • 要求ブロッキング
  • この機能は、要求の送信時にトリガーする.
    axios.interceptors.request.use(function (config) {
      let token = window.localStorage.getItem("token");
          if (token) {
              config.headers.token = token;    // token           
          }
         return config; //       config
        }, function (error) {
           return Promise.reject(error);
    });