依據官網的生命周期,數據更新時,相關的組件生命周期包括 beforeUpdate 、 updated 。
| 生命周期 | 描述 |
| beforeUpdate | 數據更新前調用。 |
| updated | 由於數據更改導致的虛擬 DOM 重新渲染和打補丁,在這之后會調用該鈎子。 |
$forceUpdate 迫使組件或實例強制渲染。

示例
方法用於更新數據。當執行 this.$forceUpdate() 時,重新刷新數據。(輸出“更新了數據”)
1 <template> 2 <view class="flex flex-direction"> 3 <view>姓名:{{ student.name }}</view> 4 <view>年齡:{{ student.age }}</view> 5 <button class="margin-top cu-btn bg-blue" @click="addAge">年齡+1</button> 6 <button class="margin-top cu-btn bg-blue" @click="reload">更新數據</button> 7 </view> 8 </template> 9 10 <script> 11 export default { 12 name:"comp-x", 13 data() { 14 return { 15 student:{ 16 name :'張三', 17 age:15 18 } 19 } 20 }, 21 updated:function(){ 22 console.log("更新了數據"); 23 }, 24 methods:{ 25 addAge: function(){ 26 this.student.age += 1; 27 }, 28 reload: function(){ 29 this.$forceUpdate(); 30 } 31 } 32 } 33 </script>

