路由器推送不适用于Vuex和JS Promise

时间:2018-07-20 05:20:15

标签: javascript vue.js vue-router vuex

这是store.js,用于处理vuex存储以及登录和注销功能,

actions:{

    login({ commit }, creds){
        commit(LOGIN);
        return new Promise((resolve) => {
            setTimeout(() =>{
                axios.post('/oauth/token', creds).then((response) =>{
                Vue.auth.setToken(response.data.access_token, response.data.expires_in + Date.now());
                commit(LOGIN_SUCCESS);
                });
                resolve();
            }, 3000);
        });
    },

    logout({commit}){
        return new Promise((resolve) => {
            setTimeout(() =>{
                Vue.auth.destroyToken();
                commit(LOGOUT);
                resolve();
            }, 3000);
        });
        console.log('Successfully logged out');

    }

}

和登录组件是这个,

login(){
    try{
       this.$store.dispatch('login', {
       username: this.user.email,
       password: this.user.password,
       client_id: 2,
       client_secret: 'wcw9GkgKMHXUnyZavawJWXgE3GhSubOADO6tKw99',
       grant_type: 'password'
     })
    } catch(error) {
          console.log('error');
      }
      finally{
        alert('Router Push')
          this.$router.push('/dashboard');
          console.log('success')
      }
  }

无论我在哪里放置router.push都不起作用,但是alertconsole.log正常工作,但是页面没有推送到指定的路径,这在页面刷新后仍然有效。但警报弹出后不久。怎么了预先感谢。

这是导航卫士

router.beforeEach((to, from, next) =>{
 if(to.matched.some(record => record.meta.forVisitor)){
      if(Vue.auth.isAuthenticated())
      {
           next({
               path: '/dashboard'
           });
      } else {
           next();
      }
 } else if(to.matched.some(record => record.meta.isauth)){
      if( !Vue.auth.isAuthenticated())
      {
           next({
               path: '/login'
           });
      } else {
           next();
      }
 } else {
      next()

 }
});

0 个答案:

没有答案