vue監(jiān)視器@Watch創(chuàng)建執(zhí)行immediate方式
vue監(jiān)視器@Watch創(chuàng)建執(zhí)行immediate
vue監(jiān)視器@Watch的immediate參數(shù),表示在watch中首次綁定的時候,是否執(zhí)行handler函數(shù),
1.值為true則表示在watch中聲明的時候,就立即執(zhí)行handler方法,
2.值為false,則和一般使用watch一樣,在數(shù)據(jù)發(fā)生變化的時候才執(zhí)行handler。
async created() { // await this.refreshMessageBody() } @Watch('message',{ immediate:true, // immediate表示在watch中首次綁定的時候,是否執(zhí)行handler, // 值為true則表示在watch中聲明的時候,就立即執(zhí)行handler方法, // 值為false,則和一般使用watch一樣,在數(shù)據(jù)發(fā)生變化的時候才執(zhí)行handler。 }) async refreshMessageBody(){ console.log("message改變了"); await this.func1(); await this.func2(); }
vue監(jiān)視屬性(watch)
監(jiān)視屬性
監(jiān)視屬性watch:
1.當(dāng)被監(jiān)視的屬性變化時,回調(diào)函數(shù)自動調(diào)用,進(jìn)行相關(guān)操作
2.監(jiān)視的屬性必須存在,才能進(jìn)行監(jiān)視
3.監(jiān)視的兩種寫法:
- new Vue時傳入watch屬性
- 通過app.$watch監(jiān)視
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>監(jiān)視</title> <script src="../js/vue.min.js"></script> </head> <body> <div id="app"> <!--第一種寫法--> <h2>今天天氣很{{isHot ? '炎熱':'涼爽'}}</h2> <!--第二種寫法:通過計算屬性--> <h2>今天天氣很{{info}}</h2> <!--綁定methods的方法--> <button @click="change">點擊切換天氣</button> <!--離譜寫法:適用于簡單測試--> <button @click="isHot = !isHot">點擊切換天氣</button> </div> <script> let app = new Vue({ el: '#app', data: { isHot: true }, methods: { change: function (){ this.isHot = !this.isHot } }, computed: { info: { get: function (){ if (this.isHot==true){ return '炎熱' }else{ return '涼爽' } }, set: function (){ this.isHot = !this.isHot } } }, watch: { //監(jiān)視誰 isHot: { //什么時候調(diào)用?當(dāng)isHot改變 handler(newValue,oldValue) { console.log('isHot被修改惹',newValue,oldValue) }, //初始化時讓handler調(diào)用一下 immediate: true } } }) //可以替換上面的watch屬性 app.$watch('isHot',{ //什么時候調(diào)用?當(dāng)isHot改變 handler(newValue,oldValue) { console.log('isHot被修改惹',newValue,oldValue) }, //初始化時讓handler調(diào)用一下 immediate: true }) </script> </body> </html>
深度監(jiān)視
深度監(jiān)視:
? 1.vue中的watch默認(rèn)不監(jiān)測對象內(nèi)部值的改變(一層)
? 2.配置deep:true可以檢測對象內(nèi)部值改變(多層次)
備注:
? 1.vue自身可以監(jiān)測對象內(nèi)部值的改變,但vue提供的watch默認(rèn)不可以
? 2.使用watch時根據(jù)數(shù)據(jù)的具體結(jié)構(gòu),決定是否采用深度監(jiān)視
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>監(jiān)視簡寫形式</title> <script src="../js/vue.min.js"></script> </head> <body> <div id="app"> <h2>今天天氣很{{isHot ? '炎熱':'涼爽'}}</h2> <button @click="change">點擊切換天氣</button> </div> <script> let app = new Vue({ el: '#app', data: { isHot: true, numbers: { a: 1, b: 2 } }, methods: { change: function (){ this.isHot = !this.isHot } }, computed: { info: { get: function (){ if (this.isHot==true){ return '炎熱' }else{ return '涼爽' } }, set: function (){ this.isHot = !this.isHot } } }, watch: { //完整寫法 // isHot: { // // deep: true,深度監(jiān)視 // handler(newValue,oldValue) { // console.log('isHot被修改惹',newValue,oldValue) // }, // //初始化時讓handler調(diào)用一下 // // immediate: true // }, //簡寫 isHot(newValue,oldValue){ console.log('isHot被修改了',newValue,oldValue) } } }) //完整寫法 app.$watch('isHot',{ //什么時候調(diào)用?當(dāng)isHot改變 handler(newValue,oldValue) { console.log('isHot被修改惹',newValue,oldValue) }, //初始化時讓handler調(diào)用一下 immediate: true }) //不允許寫箭頭函數(shù),會導(dǎo)致this指向的不是app, // vue管理的函數(shù)(methods中的,computed,watch中的)都得寫普通函數(shù), // 不能寫es6里的箭頭函數(shù) app.$watch('isHot',function(newValue,oldValue) { console.log('isHot被修改惹',newValue,oldValue) }) </script> </body> </html>
監(jiān)視效果:
computed VS watch
計算屬性在大多數(shù)情況下更合適,但有時也需要一個自定義的偵聽器。
這就是為什么 Vue 通過 watch
選項提供了一個更通用的方法,來響應(yīng)數(shù)據(jù)的變化。
當(dāng)需要在數(shù)據(jù)變化時執(zhí)行異步或開銷較大的操作時,這個方式是最有用的
computed與watch區(qū)別
- computed能完成的功能,watch都可以完成
- watch能完成的功能,computed不一定能完成,例如:watch可以進(jìn)行異步操作
注意:
- 所被Vue管理的函數(shù),最好寫成普通函數(shù),這樣this的指向才是vue實例對象或組件實例對象
- 所有不被Vue所管理的函數(shù)(定時器的回調(diào)函數(shù)、ajax的回調(diào)函數(shù)等),最好寫成箭頭函數(shù),這樣this的指向才是vue的實例或組件的實例對象
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue項目webpack中Npm傳遞參數(shù)配置不同域名接口
這篇文章主要介紹了vue項目webpack中Npm傳遞參數(shù)配置不同域名接口,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06對vue2.0中.vue文件頁面跳轉(zhuǎn)之.$router.push的用法詳解
今天小編就為大家分享一篇對vue2.0中.vue文件頁面跳轉(zhuǎn)之.$router.push的用法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08IE11下處理Promise及Vue的單項數(shù)據(jù)流問題
最近我開發(fā)的公司的競賽網(wǎng)站被發(fā)現(xiàn)在IE11下排行榜無數(shù)據(jù),但是在其他瀏覽器沒問題,我然后打開控制臺一看,發(fā)現(xiàn)有問題,糾結(jié)了半天才搗騰好,下面小編把方案分享處理,供大家選擇2019-07-07在Vue?3中使用OpenLayers加載GPX數(shù)據(jù)并顯示圖形效果
本文介紹了如何在Vue 3中使用OpenLayers加載GPX格式的數(shù)據(jù)并在地圖上顯示圖形,通過使用OpenLayers的GPX解析器,我們能夠輕松地處理和展示來自GPS設(shè)備的地理數(shù)據(jù),需要的朋友可以參考下2024-12-12antd中table展開行默認(rèn)展示,且不需要前邊的加號操作
這篇文章主要介紹了antd中table展開行默認(rèn)展示,且不需要前邊的加號操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11