0

我使用vue-authenticate ( https://github.com/dgrubelic/vue-authenticate ) 使用 ID/Password 和 Oauth 1 & 2 登录。

我在哪里放置路由器重定向以在仪表板页面上重定向用户?

this.$router.push({name: 'dashboard'})

我的代码 store.js 与 Vuex:

import Vue from 'vue'
import Vuex from 'vuex'
import {vueAuth} from './auth'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    isAuthenticated: false
  },
  getters: {
    isAuthenticated () {
      return vueAuth.isAuthenticated()
    }
  },
  mutations: {
    isAuthenticated (state, payload) {
      state.isAuthenticated = payload.isAuthenticated
    },
    setProfile (state, payload) {
      state.profile = payload.profile
    }
  },
  actions: {
    login (context, payload) {
      payload = payload || {}
      return vueAuth.login(payload.user, payload.requestOptions).then((response) => {
        context.commit('isAuthenticated', {
          isAuthenticated: vueAuth.isAuthenticated()
        })
      })
    }
  }
})
4

1 回答 1

0

你可以在你的 vue 组件中调度你的动作后使用 .then() ,你可以在你的调度动作完成后放置 $router.push() 方法。

// SomeComponent.vue
.
.
.
methods: {
  login () {
    this.$store.dispatch('login', payload)
      .then(() => {
        this.$router.push({ name: 'dashboard' })
      })
  }
}

或者你可以在你的 actions.js 文件中使用,但我在 vue 中做 $router 工作

// actions.js
import Vue from 'vue'

const actions = {
  login (context, payload) {
    payload = payload || {}
    return vueAuth.login(payload.user, payload.requestOptions)
      .then((response) => {
        context.commit('isAuthenticated', {
        isAuthenticated: vueAuth.isAuthenticated()
        Vue.$router.push({ name: 'dashboard' })
      })
    })
  }
}
于 2017-10-27T16:53:02.513 回答