关于前端:Vue-强制组件重新渲染

在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;
    });
  }

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理