比如 :
<add-roles v-if="flag" :id="current_id" ></add-roles>
有個函數把flag 設置爲true 子組件顯示
look_role(id){
this.flag = true
this.current_id =id
},
子組件
props:["id"],
watch:{
'id':function(newVal,oldVal){
console.log("sdfsadf",newVal)
},
你會發現根本監聽不到 id 的變化
這是因爲v-if 會影響 子組件的生命週期 導致watch 不起作用
把 v-if 換成 v-show 就解決了。