I have a vue application and I want somehow save or memorize the last visited page with vuex-persistentstate. What I want is: I have 3 pages for example A,B,C and all three leads to Page D and on Page D there is a "cancel/finish" button. Clicking it now, redirect to Home Page. But I want to return to the last visit page depending where I was (A,B or C).
I want to realize it with vuex-persistentstate
So far I have this.
src/store/index.js
export default createStore({
plugins: [
createPersistedState({
key: 'xx',
paths: [
'global.pageAfterFinishClick',
...,
],
}),
],
And here src/store/global/index.js
state: {
pageAfterFinishClick: '/'
...
}
I think next step is to add mutation and action and update my router?
mutations: {
// Other mutations...
[SET_PAGE_AFTER_FINISH_CLICK](state, payload) {
state.pageAfterFinishClick = payload;
},
}
and
actions: {
// Other actions...
setPageAfterFinishClick({ commit }, page) {
commit(SET_PAGE_AFTER_FINISH_CLICK, page);
},
}
I'm struggling with this step and need your help.
This is the way I do it:
Vuex store
And then I Update
routersetup to dispatch theupdatePreviousRouteaction whenever a route navigation occurs:With this setup, every time a route navigation occurs, the
updatePreviousRouteaction is dispatched with the previous route information, and it updates the Vuex store with the previous route.And then I use the
previousRouteproperty from theVuex storeto navigate back:This will navigate to the previous page if available, or to the home page ('/') if there is no previous route stored.