繁体   English   中英

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

[英]Vue Router this.$router.push not working on methods

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

我的代码:

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

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

我的attemptLogin是一个动作,它的代码如下:

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

就我而言,我发现我的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()等。 但我不想不把它放在那里给可能需要它的人。 我的菜鸟错误。

我有同样的问题 this.$router.push('/') 似乎不起作用,因为我没有重定向,日志中也没有错误。 我没有意识到的是,我有一小块中间件正在查看是否设置了身份验证令牌。 我把它看错了商店,所以自然它总是会返回瀑布。 如果您有中间件,请记住查看您的中间件,因为它不会在 router.push() 上返回错误

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

就我而言,它不工作的原因是因为我试图在更新状态之前将 $router.push 到受保护的路由。 *更具体地说,我依赖 Firebase onAuthStateChanged 来更新状态,并且在我无法控制的此操作之前尝试 $router.push。 *

建议:

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

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

希望能帮助到你。 干杯

我有同样的问题。 确保在路由器中设置mode: 'history'

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

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

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

为了处理这种情况,您可以在方法中使用以下技术/代码:

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: '/'} );
}

我使用that = this从 global scope 获取了 global this的副本,然后使用that.$router.push('/path')

这对我有用!

前:

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

后:

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

使用$nuxt.$router.push代替this.$router.push

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM