vue如何使用watch監(jiān)聽指定數(shù)據(jù)的變化
更新時間:2022年04月08日 14:17:53 作者:北海之靈
這篇文章主要介紹了vue如何使用watch監(jiān)聽指定數(shù)據(jù)的變化,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
使用watch監(jiān)聽指定數(shù)據(jù)的變化
在vue中,可以使用watch屬性來監(jiān)聽data中某個屬性值的變化。
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <div id='app'> <input type="text" v-model="firstname" >+ <input type="text" v-model="lastname" >= <input type="text" v-model="fullname"> </div> </body> <script src="../lib/vue.js"></script> <script> var vm = new Vue({ el:'#app', data:{ firstname:'', lastname:'', fullname:'' }, methods:{ }, //使用這個屬性,可以監(jiān)視 data 中指定數(shù)據(jù)的變化,然后觸發(fā)這個 watch 中對應(yīng)的function 處理函數(shù) watch:{ firstname:function(newVal,oldVal){ //console.log('監(jiān)視到了firstname的變化'); //this.fullname = this.firstname + "-" + this.lastname console.log(newVal +"---"+oldVal) this.fullname = newVal + "-" + this.lastname }, 'lastname':function(newVal){ this.fullname = this.firstname + "-" + newVal } } }) </script> </html>
vue watch監(jiān)聽多個值
用computed定義一個address對象吧,然后再去watch addres
data() { return { check: false, sign_img: "", submit_flag: false' } }, computed: { btnObj() { const { sign_img, check } = this return { sign_img, check } } }, watch: { btnObj: { handler: function(newVal,oldVal) { if(!!this.sign_img && this.check){ this.submit_flag = true this.sign_flag = '1' }else{ this.submit_flag = false this.sign_flag = '0' } }, deep: true, immediate: true } }
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue 開發(fā)音樂播放器之歌手頁右側(cè)快速入口功能
這篇文章主要介紹了Vue 開發(fā)音樂播放器之歌手頁右側(cè)快速入口功能,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-08-08Vue Router4中params傳參失效和報錯問題的解決方法
在使用vue-router4中params 進(jìn)行路由組件之間傳參,跳轉(zhuǎn)頁面接收不了并報錯,本文給大家介紹了Vue Router4中params傳參失效和報錯問題的解決方法,需要的朋友可以參考下2024-03-03Vue開發(fā)配置tsconfig.json文件的實(shí)現(xiàn)
tsconfig.json文件中指定了用來編譯這個項(xiàng)目的根文件和編譯選項(xiàng),本文就來介紹一下Vue開發(fā)配置tsconfig.json文件的實(shí)現(xiàn),感興趣的可以了解一下2023-08-08