Vue Router this.$router.push 不处理方法

Vue Router this.$router.push not working on methods

我正在登录,但在登录方法成功后,我正在为 $router 对象推送路由,但它不起作用,有人可以帮助我吗?

我的代码:

doLogin(){
       this.attemptLogin({...this.user}).then(function(){
            this.$router.push('/') 
            } )
  },

因此,登录方法按预期执行,但回调 this.$router.push('/') 没有 运行。

我的attemptLogin是一个action,代码如下:

export const attemptLogin = ( {commit}, user) => {
    return http.post('login', {
        'email': user.email,
        'password': user.password
    }).then(response => {
        return commit(types.setToken, response.data)
    }).catch( e => console.log(e))
}

已解决,我将代码中的方法 this.$router.push() 更改为 this.$router.go('/')

感谢大家的评论。

您必须推送到名称或路径 试试这个:

this.$router.push({ path: '/' })
this.$router.push({ name: 'Home' })

我遇到了同样的问题。确保在路由器

中设置 mode: 'history'

在我的例子中,我发现我的beforeRouteUpdate没有执行next()方法

差:

beforeRouteUpdate(to, from, next) {
  /*
  something...
  */
}

好:

beforeRouteUpdate(to, from, next) {
  /*
  something...
  */

  next() // DO IT!
}

我知道这个话题有点老了,但是遇到这个问题,特别是在 Nuxt 和 Vue 中,我只想提供一些可能对某些人有所帮助的更新。

login() {
    this.$auth.loginWith('local', {
        data: {
            username: this.username,
            password: this.password,
        }
    });
    this.$router.push({name: 'dashboard'});
}

我的代码最初是这样的,但我忘记确保我使用的是 Promise 或 Async / Await 方法。

正确的代码(在我的例子中)是这样的;

async login() {
    await this.$auth.loginWith('local', {
        data: {
            username: this.username,
            password: this.password,
        }
    });
    this.$router.push({name: 'dashboard'});
}

如我所说,如果需要,您可以使用 .then() 等。但我不想把它放在那里给任何可能需要它的人。我的菜鸟错误。

我不太喜欢 $router.go 方法,因为它会完全刷新页面。

在我的例子中,它不起作用的原因是因为我在更新状态之前试图 $router.push 到受保护的路线。 *更具体地说,我依赖于 Firebase onAuthStateChanged 来更新状态,并且我试图在我无法控制的这个动作之前 $router.push 。 *

建议:

  • 在尝试推送到该路由之前,请确保您的中间件所依赖的状态(在您的受保护路由上)已提交。
  • 推送到像 'index' 这样的不受保护的路由,并使您的组件根据状态值进行反应。 (不是最好的方法)

    • 我最终使用 Firebase Auth 做的是创建另一个动作,提交与我的 firebase onAuthStateChanged 提交相同的状态,但我自己在我的异步函数中完成。

希望对您有所帮助。干杯

我遇到了同样的问题。$router.push('/') 似乎无法正常工作,因为我没有重定向,日志中也没有错误。我没有意识到的是,我有一小块中间件正在查看是否设置了授权令牌。我让它在错误的商店看,所以它自然总是 return 掉下来。如果你有中间件,请记得查看它,因为它不会 return router.push()

上的错误

之前:

    doLogin(){
    this.attemptLogin({...this.user})
    .then(function(){
    this.$router.push('/') 
    } )
    },

之后:

    doLogin(){
    this.attemptLogin({...this.user})
    .then(() => {
    this.$router.push('/') 
    } )
    },

实际上,您可以使用以下代码在 Nuxtjs 中更改路由。

this.$router.push("/");
or
this.$router.push({ path: '/'} );

假设,如果您现在查看此 link“http://localhost:3000”或“http://192.168.0.102:300”并尝试使用给定代码通过推送更改路由路由“/”。所以它不会工作。因为,您已经在查看这条路线。所以,Nuxtjs 路由不能改。

为了处理这种情况,您可以在方法中使用以下 technique/code:

vuejs 方式:

if($route.path == '/') {
   //following 2 line code are sample code here put your need codes
   this.isLogin = false;
   this.user_full_name = 'My Account';
} else {
   // this.$router.push("/");
   this.$router.push({ path: '/'} );
}

=============================================

nuxtjs 方式:

if($nuxt.$route.path == '/') {
   //following 2 line code are sample code here put your need codes
   this.isLogin = false;
   this.user_full_name = 'My Account';
} else {
   // this.$router.push("/");
   this.$router.push({ path: '/'} );
}