admin管理员组文章数量:1321259
Trying to push route change from within the store. I tried importing router
into the store itself and then doing something along the lines of:
LOG_OUT({mit}){
mit('LOG_OUT__MUTATION');
router.push({
name: 'Login'
})
}
... but that didn't work.
I also don't want to push a route change from directly with the ponents because it would mean I need to check authentication and push a route change in every ponent, which seems cumbersome to me.
Edited:
The LOG_OUT__MUTATION
clears the token stored in the state.
Trying to push route change from within the store. I tried importing router
into the store itself and then doing something along the lines of:
LOG_OUT({mit}){
mit('LOG_OUT__MUTATION');
router.push({
name: 'Login'
})
}
... but that didn't work.
I also don't want to push a route change from directly with the ponents because it would mean I need to check authentication and push a route change in every ponent, which seems cumbersome to me.
Edited:
The LOG_OUT__MUTATION
clears the token stored in the state.
-
please explain and include the
LOG_OUT__MUTATION
code – Daksh M. Commented Mar 2, 2018 at 7:36 - Just edited my question :) it basically just clears the Auth token from the store. – Modermo Commented Mar 2, 2018 at 7:45
- correct me if im wrong, it is going to the login view page, before miting to the store right? that means router is pushing before the mit, hence this is an async problem. – Daksh M. Commented Mar 2, 2018 at 7:48
- No, it wipes the token from the store, THEN redirects to login. – Modermo Commented Mar 2, 2018 at 9:59
2 Answers
Reset to default 6I finally figured this out.
Final Implementation
In your store
, simply import the router
:
import router from '@/router/index'
actions: {
LOG_OUT({mit}){
mit('LOG_OUT__MUTATION');
router.push({
name:'Login'
})
}
}
And then from another part of my app, I simply imported store
and dispatched an action:
import Store from '@/store/store'
myFunction () {
Store.dispatch('LOG_OUT');
}
Initial Implementation
Here was my implementation of Daksh Miglani's solution (which works). In my store.js, I had:
export const forceLogout = (mit) => {
return new Promise((resolve, reject) => {
mit('LOG_OUT__MUTATION');
resolve();
})
}
export default new Vuex.Store({
actions,
mutation
})
And then in another part of the app (helpers.js), I had:
import Store from '@/store/store'
import { forceLogout } from '@/store/store'
import router from '@/router/index'
forceLogout(Store.mit)
.then(() => {
debugger;
router.push({
name:'Login'
})
}).catch((error) => {
console.log(error)
})
Okay, I have a solution for you buddy:
So firstly you write a store mitting function like this:
export const mitLogout = ({mit}) => {
return new Promise((resolve, reject) => {
mit('LOG_OUT__MUTATION');
// once you're done with your clearing, resolve or reject the promise
resolve();
});
}
This function will return a promise, that you can use in your logging out function which will at the end allow you to mit and clear first and then logout.
Then you use this function as your logout function:
...
logout() {
mitLogout().then(() => {
this.$router.push({
name: 'Login'
})
})
}
...
本文标签: javascriptPush route from within action in Vuex StoreStack Overflow
版权声明:本文标题:javascript - Push route from within action in Vuex Store - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742028913a2416072.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论