在vue 中我们可能会有很多业务引用一个组件

举个例子

比如数学有10中不同类型的tab题,但是引用的是一个公共组件
这个时候我们tab切换的时候就会报出组件的name 值错误,或者组件并没有重新渲染,
这时候我们需要销毁组件

方式一

利用key值

<component 
:is="getComponentTag(bookContent.type)"
:key="componentKey">
  data: function () {
    return {
      componentKey:0,
    };
  },
  tabClick: function (item) {
    this.componentKey += 1;
  }

方法二

v-if
<component v-if="renderComponent">
tabClick: function (item) {
 this.renderComponent = false;    
    this.$nextTick(() => {
      // 在 DOM 中添加 component 组件
      this.renderComponent = true;
    });
  }

小寒大大
1 声望0 粉丝