我使用vue-authenticate (https://github.com/dgrubelic/vue-authenticate)使用ID/密码和Oauth 1& 2登录。
我把路由器重定向到仪表板页面上重定向用户的位置?
this.$router.push({name: 'dashboard'})用Vuex::编写代码store.js
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()
})
})
}
}
})发布于 2017-10-27 16:53:02
您可以在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' })
})
})
}
}https://stackoverflow.com/questions/46976652
复制相似问题