简体   繁体   English

在Nuxt JS vuex中使用Vue Router进行重定向

[英]Redirect using Vue Router in Nuxt JS vuex

I'm building a login/register system as part of an app. 我正在将登录/注册系统构建为应用程序的一部分。 I'm using Firebase and Vuex to handle authentication and database information. 我正在使用Firebase和Vuex处理身份验证和数据库信息。 I have some actions in Nuxt JS that create the user, sign them in and log them out. 我在Nuxt JS中有一些操作可以创建用户,登录并注销他们。

I'm trying to implement a redirect after the user has been successfully registered / when they click login, my code is: 我正在尝试在用户成功注册后实现重定向/当他们单击登录时,我的代码是:

export const actions = {

  /*
   * Create a user
   */
  createUser ({commit}, payload) {
    this.$fireAuth.createUserWithEmailAndPassword(payload.email, payload.password).then(function(firebaseUser) {
      commit('setUser', payload)
    }).catch(function(error) {
      console.log('error logging in' + error)
    });
  },

  /*
   * Log a user into Beacon
   */
  login ({commit}, payload) {
    this.$fireAuth.signInWithEmailAndPassword(payload.email, payload.password).then(function(firebaseUser) {
      commit('setUser', payload)
    }).catch(function(error) {
      console.log('error logging in' + error)
    });
  },

  /*
   * Sign a user out of Beacon
   */
  signOut ({commit}) {
    this.$fireAuth.signOut().then(() => {
      commit('setUser', null)
    }).catch(err => console.log(error))
  }

}

I'm using Nuxt JS 2.9.2, on Vue JS 2.6.10 我在Vue JS 2.6.10上使用Nuxt JS 2.9.2

I have a few modules. 我有几个模块。

I've tried using this.router.push('/') and window.location.href , but would like to retain the SPA functionality. 我尝试使用this.router.push('/')window.location.href ,但想保留SPA功能。

You have two ways to do it: 您有两种方法可以做到:

  1. $nuxt.$router.push in actions $nuxt.$router.push操作
  2. Pass this.$router.push as argument in your action, and call where you need. 在操作中传递this.$router.push作为参数,然后在需要的地方调用。

Hope this helps. 希望这可以帮助。

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

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