【Vue】组件的刷新

  1. 刷新整个页面,
    通过刷新整个页面达到组件的刷新,例如router上的go方法:this.router.go(0),或者location.reload(),缺点页面会出现空白。刷新页面会触发组件的beforeCreatecreatebeforeMountmountedbeforeDestorydestoryed方法。不推荐。

  2. 使用v-if刷新组件,
    v-if:在切换时元素及它的数据绑定 / 组件被销毁并重建。如果元素是 <template>,将提出它的内容作为条件块。
    使用v-if指令控制组件的刷新,这实际上就是控制组件的条件渲染,组件会被销毁和重建,当然也会触发组件的beforeCreatecreatebeforeMountmountedbeforeDestorydestoryed方法

  3. 使用组件内置vm.$forceUpdate()方法,
    $forceUpdate()让 Vue实例重新渲染(rander)。注意它仅仅影响实例本身和插入插槽内容的子组件,而不是所有子组件。会触发beforeUpdateupdated方法

  4. 使用特殊attribute key
    key主要用在 Vue 的虚拟 DOM 算法,在新旧 nodes 对比时辨识 VNodes。key的改变 Vue 会重新渲染组件,详见。其会触发组件的beforeCreatecreatebeforeMountmountedbeforeDestorydestoryed方法。

<article :key="new Date().getTime()"> xxxxx </article>

猜你喜欢

转载自blog.csdn.net/qq_43662579/article/details/128664018
今日推荐