vue-authenticate和路由器重定向

时间:2017-10-27 13:49:27

标签: vue.js vuex

我使用 vue-authenticate https://github.com/dgrubelic/vue-authenticate)登录ID /密码和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()
        })
      })
    }
  }
})

1 个答案:

答案 0 :(得分: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' })
      })
    })
  }
}