Components in Vue are not destroyed when switching

problem description

wrote a process class application, and the templates are all reused. Suppose there are three types of template switching. If there are two identical templates in a row, the template cache will not be destroyed

.

related codes

the simplified code is as follows

<div id="dynamic-component-demo">
  <button v-for="tab in tabs" v-bind:key="tab" @click="currentTab = tab">{{ tab }}</button>
  <component :is="currentTabComponent" ></component>
</div>
Vue.component("tab-posts", {
  data: function() {
    return {
            args:"",
    }
  },
  template:"<div><input type="text" v-model="args"></div>",
})

Vue.component("tab-archive", {
  template: "<div>Archive component</div>"
})

new Vue({
  el: "-sharpdynamic-component-demo",
  data: {
    currentTab: "Posts",
    tabs: ["Posts", "Posts", "Archive", "Posts"]
  },
  computed: {
    currentTabComponent: function() {
      return "tab-" + this.currentTab.toLowerCase()
    }
  }
})

switching between different components will completely destroy the previous variables. if you switch to the same component, the component will not be destroyed. Initialize the variable after each click. But the actual scene is not universal.

Apr.07,2021

new Vue({
  el: '-sharpdynamic-component-demo',
  data: {
    currentTab: 'Posts',
    tabs: ['Posts', 'Posts', 'Archive', 'Posts'],
    currentTabComponent: 'tab-posts'
  },
  computed: {
    
  },
  methods: {
      changeTab (tab) {
        this.currentTab = tab
      this.currentTabComponent = ''
      this.$nextTick(() => {
          this.currentTabComponent = 'tab-' + this.currentTab.toLowerCase()
      })
      
    }
  }
})
<div id="dynamic-component-demo">
  <button v-for="(tab, index) in tabs" v-bind:key="index" @click="changeTab(tab)">{{ tab }}</button>
  <component :is="currentTabComponent" ></component>
</div>

the method downstairs is better. If you report a mistake, wrap a div around button or component and you will not make a mistake

.

give each component a different key, so that the same components can be distinguished.

<button v-for="(tab, index) in tabs" v-bind:key="index" @click="currentTabIndex = index">{{ tab }}</button>
<component :is="currentTabComponent" :key="currentTabIndex" ></component>


new Vue({
  el: '-sharpdynamic-component-demo',
  data: {
    currentTabIndex: 0,
    tabs: ['Posts', 'Posts', 'Archive', 'Posts']
  },
  computed: {
    currentTabComponent: function() {
      return 'tab-' + this.tabs[currentTabIndex].toLowerCase()
    }
  }
})
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3c15d-40871.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3c15d-40871.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?