简体   繁体   English

Vue.js - 删除从保持活动加载的子组件

[英]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我的问题类似于此处列出的问题: 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:我还使用 Vue 路由器创建了一种选项卡系统,因此代码如下所示:

//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: vue-router 使用的路由的轮廓如下所示:

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

Keep alive is being used to maintain State when switching tabs (switching childroutes).在切换选项卡(切换子路由)时,保持活动状态用于维护 State。

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.我想在关闭选项卡时从缓存中删除 childRoute/Component 但在我的选项卡组件中使用this.$destroy似乎正在破坏整个选项卡组件,而不是其中的子组件。

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.这个问题和其他堆栈溢出问题中也说明的 V-if 解决方案将不起作用,因为我只想从 memory 中删除这个特定的选项卡,这会删除所有选项卡。

Thanks for any help.谢谢你的帮助。

I found a solution using the include argument in keep-Alive https://v2.vuejs.org/v2/api/#keep-alive我在 keep-Alive https://v2.vuejs.org/v2/api/#keep-alive中找到了使用include参数的解决方案

I kept an array of currently active Tabs using router.getmatchedcomponents() to get the component name of the currently opened tab.我使用router.getmatchedcomponents()保留了一组当前活动的选项卡,以获取当前打开的选项卡的组件名称。

And then in my handleClose() function, I remove the tab that has been closed from the include array.然后在我的handleClose() function 中,我从include数组中删除已关闭的选项卡。

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>
deactivated() {
    this.$destroy();
},

in a child component works perfectly fine.在子组件中工作得很好。

While working with Parent and Child components, I've almost always have to use vuex to update the chain processes from happening with updates to any of the underlying components rendered on the page.在使用父组件和子组件时,我几乎总是必须使用 vuex 来更新链流程,以免发生在页面上呈现的任何底层组件的更新。

After making updates to the Vuex , I usually update the key of the component in context to re-render it.在对Vuex进行更新后,我通常会在上下文中更新组件的以重新渲染它。

Vuex seems to resolve the state management issues in most cases and you won't have to use keep-alive after that.在大多数情况下,Vuex 似乎可以解决 state 管理问题,之后您就不必使用keep-alive

It always solves the problem.它总能解决问题。 I'm hoping this could help you if I've understood your problem.如果我了解您的问题,我希望这可以帮助您。

If I've misunderstood it please comment and I'll remove it.如果我误解了它,请发表评论,我会删除它。

Why don't you remove the child from the router that should automatically update your component you can try router.replaceRoutes(routes)你为什么不从应该自动更新你的组件的路由器中删除孩子你可以试试router.replaceRoutes(routes)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM