8

I have a Vue.js SPA that uses vue-router. because after logging in each page needs to keep state (like filters/pagination entered) This is done using keep-alive:

<keep-alive>
  <router-view/>
</keep-alive>

This works very well but when somebody logs out and logs in again I still see the same filter values due to the keep alive.

Can I refresh/purge the pages kept alive programatically somehow at logout?

3 Answers 3

9

I don't think there is an easy way to do this(programmatically) so you are left with 2 options.

Options 1 would be to reset the state which means you internally keep track of the initial state and listen for something like an emit on logout to reset the component to its initial state.

Option 2 would be to simply swap out the keep-alive component using something like this:

<keep-alive v-if="loggedIn">
    <router-view></router-view>
</keep-alive>

<router-view v-else></router-view>
Sign up to request clarification or add additional context in comments.

2 Comments

I used option 2. This way all information is reset after another reload (also in vue-good-table where I do not have full access)
Option 2 is a nice trick that I hadn't thought of before. Must come in handy with dynamic components. +1
4

One way you can use Vue global event.

Vue.prototype.$loginState = new Vue();

In your component fire the event on click logout button.

function logout(){
    //your codes here to logout and then
    this.$loginState.$emit('logout');
}

In component which user sees when logged in just set all variables to their default values by listening event fired 'logout'.

<button @click="resetValues"> Logout </button>
methods: {
    resetValues(){
        this.yourValue = '';
        this.filters = [];
    }
}
created(){
    this.$loginState.$on('logout', this.resetValues)
}

Comments

2

The kept alive components are inside this.$children, so you can probably traverse those and modify the state.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.