方案一:v-if(可以重置生命周期)
當數據變更后,通過watch 監聽,先去銷毀當前的組件,然后再重現渲染。使用 v-if 可以解決這個問題
<template> <third-comp v-if="reFresh"/> </template> <script> export default{ data(){ return { reFresh:true, menuTree:[] } }, watch:{ menuTree(){ this.reFresh= false this.$nextTick(()=>{ this.reFresh = true }) } } } </script>
這種方式雖然可以實現,但太不優雅
方案二 ::key=‘’(此處可觸發watch和update)
通過vue key 實現,原理請查看官方文檔。所以當key 值變更時,會自動的重新渲染。
<template> <third-comp :key="menuKey"/> </template> <script> export default{ data(){ return { menuKey:1 } }, watch:{ menuTree(){ ++this.menuKey } } } </script>
方案三:this.$forceUpdate
這個方法可以使當前組件調用這個方法時,重新渲染組件。