【问题标题】:Vue Router this.$router.push not working on methodsVue Router this.$router.push 对方法不起作用
【发布时间】:2018-11-10 18:38:28
【问题描述】:

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

我的代码:

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

【问题讨论】:

  • 你需要从http.post返回promise。所以应该是return http.post(...)
  • @Bert 我做到了,但没有任何反应。
  • 这里的this 是否存在范围问题?我不记得作用域是如何与() => {} 语法一起工作的。在 function() {} 语法中,你必须 .bind(this) 到函数。
  • () => {}this 绑定到父作用域,所以你之前已经正确了
  • 问题已解决,我已将 this.$router.push('/') 方法更改为 this.$route.go('/') 并且它有效。谢谢你帮助我。

标签: javascript vue.js vue-router


【解决方案1】:

其实你可以使用下面的代码在 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: '/'} );
}

【讨论】:

    【解决方案2】:

    之前:

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

    之后:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

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

        建议:

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

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

        希望对您有所帮助。干杯

        【讨论】:

          【解决方案5】:

          我知道这个话题有点老了,但是特别是在 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() 等。但我不想不把它放给任何可能需要它的人。我的菜鸟错误。

          【讨论】:

          • 当然,你完全正确,它不起作用,因为我试图在身份验证完成之前推送。我刚刚添加了对我有用的等待。
          【解决方案6】:

          就我而言,我发现我的beforeRouteUpdate 没有执行next() 方法

          不好:

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

          好:

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

          【讨论】:

          • 天哪,谢谢!!我知道这个问题不是指望Navigation Guard 成为问题的地方。但这就是我的问题所在。
          【解决方案7】:

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

          【讨论】:

            【解决方案8】:

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

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

            【讨论】:

            【解决方案9】:

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

            感谢 cmets 的每一个人。

            【讨论】:

            • 但是为什么它成功我需要从文档中弄清楚
            • 我也面临同样的问题,$router.push 在 Firefox 中不起作用。建议的 $router.go 在这里不起作用,因为这只是在浏览器历史记录中不向后和向前导航。
            • 伙计们,我不知道为什么这对我有用,但在当时,这是唯一有效的方法。
            • 文档也无济于事,因为This method takes a single integer as parameter that indicates by how many steps to go forwards or go backwards in the history stack... 所以.go('/') 不应该工作。我认为这只是重新加载页面并使其工作。除了.go() 在我的情况下也是如此。
            猜你喜欢
            • 2019-03-29
            • 2022-08-03
            • 2019-06-16
            • 2020-06-18
            • 2017-06-23
            • 2021-08-07
            • 2020-07-18
            • 2017-05-21
            相关资源
            最近更新 更多