Vue之this.$forceUpdate——強制更新數據


 依據官網的生命周期,數據更新時,相關的組件生命周期包括 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>
View Code

 

 

 

參考網址


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM