Api Vuex和路由-登录后重定向
我正在使用vuex和laravel作为我项目的后端。Api Vuex和路由-登录后重定向,api,vue.js,laravel-5.3,restful-url,vuejs2,Api,Vue.js,Laravel 5.3,Restful Url,Vuejs2,我正在使用vuex和laravel作为我项目的后端。 登录后的重定向不起作用。这是我的密码: 对于Vuex: 还有我的变异 mutations: { signin (state) { state.isLoggedIn = true } } 我的路线: export default new Router({ routes: [ { path: '/', name: 'chat', component:
登录后的重定向不起作用。这是我的密码:
对于Vuex:
还有我的变异
mutations: {
signin (state) {
state.isLoggedIn = true
}
}
我的路线:
export default new Router({
routes: [
{
path: '/',
name: 'chat',
component: Chat,
meta: {
forAuth: true
}
},
{
path: '/signin',
name: 'signin',
component: Signin,
meta: {
forVisitors: true
}
},
{
path: '/signup',
name: 'signup',
component: Signup,
meta: {
forVisitors: true
}
}
],
mode: 'history'
})
还有我的支票
router.beforeEach(
(to, from, next) => {
if (to.matched.some(record => record.meta.forVisitors)) {
if (Vue.auth.isAuthenticated()) {
next({
path: '/'
})
} else next()
} else if (to.matched.some(record => record.meta.forAuth)) {
if (!Vue.auth.isAuthenticated()) {
next({
path: '/signin'
})
} else next()
} else next()
}
)
如何自动重定向??
感谢您的帮助我不认为导航的副作用应该是Vuex store操作的一部分,除非您100%确定它始终需要作为此操作的一部分发生。无论您在应用程序中从何处执行此操作,都应注意导航。要做到这一点,你需要做两件事 在您的操作中,返回Vue.http的承诺 使用。然后在组件中调用它
//Store action, note Vue.http.post MUST return a thenable (Promise)
SIGNIN (context, user) {
context.commit('handleLoader')
return Vue.http.post(apiDomain + signInUrl, user, {headers: getHeaders})
.then(response => {
handleSettingToken(response)
return response
})
}
}
//Component
methods: {
handleLogin() {
return store.dispatch(SIGNIN, user)
.then(response => doNavigationHere(response)
}
}
你使用的是
vueRouter
吗?如果是,那么你能把你的router.js
文件放进去吗?我已经编辑了帖子,但是你的问题上没有编辑过的标签,你确定吗?看起来很好,控件是否达到了正确的如果是阻塞了?它可以工作:)我使用了“承诺”。这是链接
router.beforeEach(
(to, from, next) => {
if (to.matched.some(record => record.meta.forVisitors)) {
if (Vue.auth.isAuthenticated()) {
next({
path: '/'
})
} else next()
} else if (to.matched.some(record => record.meta.forAuth)) {
if (!Vue.auth.isAuthenticated()) {
next({
path: '/signin'
})
} else next()
} else next()
}
)
//Store action, note Vue.http.post MUST return a thenable (Promise)
SIGNIN (context, user) {
context.commit('handleLoader')
return Vue.http.post(apiDomain + signInUrl, user, {headers: getHeaders})
.then(response => {
handleSettingToken(response)
return response
})
}
}
//Component
methods: {
handleLogin() {
return store.dispatch(SIGNIN, user)
.then(response => doNavigationHere(response)
}
}