Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue.js - Remove a child component loaded from keep-alive

My question is similar to the one listed here: Vue.js - Destroy a cached component from keep alive

I am also creating a sort of Tab System using the Vue router so the code looks like so:

//My Tab component 
<template>
  <tab>
    <tab-selector />
    <keep-alive>
      <router-view />
      <!-- This router view is used to render one of the childRoutes of the main TabRoute -->
    </keep-alive> 
  </tab>
</template>

<script>
 handleTabClose() {
   //Close tab logic
   this.$destroy(); 
   //Insert router push to be one of the other tabs that have not been closed.
 }
</script>

Outline of how the route used by the vue-router would look like:

    {
        path: "/tab",
        name: "TabComponent",
        component: () => import("InsertComponentPath"),
        children: [{TabRoute1, TabRoute2, TabRoute3}]
    },

Keep alive is being used to maintain State when switching tabs (switching childroutes).

I want to remove the childRoute/Component from cache when the tab is closed but using this.$destroy in my tab component it seems to be destroying the whole Tab component and not the child component within it.

The V-if solution also stated in this and other stack overflow questions will not work as I only want to remove this specific tab from memory and this removes all tabs.

Thanks for any help.

like image 234
Jahson Avatar asked Apr 20 '21 13:04

Jahson


People also ask

How do I deactivate a component Vue?

Vue Button component can be enabled/disabled by giving disabled property. To disable Vue Button component, the disabled property can be set as true .

Does Vue router destroy component?

If we do not force Vue Router 4 to destroy the current component when we're navigating to another route, it will reuse the same component and not re-fire the created hook. We also get the user profile form saving data to the Firestore.

How do you reset a Vue component?

There are three ways to reset component state: Define key attribute and change that. Define v-if attribute and switch it to false to unmount the component from DOM and then after nextTick switch it back to true. Reference internal method of component that will do the reset.


2 Answers

I found a solution using the include argument in keep-Alive https://vuejs.org/v2/api/#keep-alive

I kept an array of currently active Tabs using router.getmatchedcomponents() to get the component name of the currently opened tab.

And then in my handleClose() function, I remove the tab that has been closed from the include array.

Final Implementation looked somewhat like so:

//My Tab component 
<template>
  <tab>
    <tab-selector />
    <keep-alive :include="cacheArr">
      <router-view />
      <!-- This router view is used to render one of the childRoutes of the main TabRoute -->
    </keep-alive> 
  </tab>
</template>

<script>
 private cacheArr = []

//Called whenever a new tab is opened
 handleOpen() {
   //Add current Tab to this.cachArr
   this.cachArr.push(router.getmatchedcomponents().pop().name);
 }

//Called whenever new tab is closed.
 handleTabClose(name) {
   //Close tab logic
   
   //Remove Tab being closed from this.cacheArr
   this.cacheArr.splice(this.cacheArr.indexOf(name), 1);
 }
</script>
like image 107
Jahson Avatar answered Sep 21 '22 05:09

Jahson


deactivated() {
    this.$destroy();
},

in a child component works perfectly fine.

like image 24
Daniel Danielecki Avatar answered Sep 20 '22 05:09

Daniel Danielecki