vue如何使用watch監(jiān)聽指定數(shù)據(jù)的變化
更新時(shí)間:2022年04月08日 14:17:53 作者:北海之靈
這篇文章主要介紹了vue如何使用watch監(jiān)聽指定數(shù)據(jù)的變化,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
使用watch監(jiān)聽指定數(shù)據(jù)的變化
在vue中,可以使用watch屬性來監(jiān)聽data中某個(gè)屬性值的變化。
<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:{ }, //使用這個(gè)屬性,可以監(jiān)視 data 中指定數(shù)據(jù)的變化,然后觸發(fā)這個(gè) watch 中對(duì)應(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)聽多個(gè)值
用computed定義一個(gè)address對(duì)象吧,然后再去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 } }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue中動(dòng)態(tài)select的使用方法示例
這篇文章主要介紹了vue中動(dòng)態(tài)select的使用方法,結(jié)合實(shí)例形式分析了vue.js使用動(dòng)態(tài)select創(chuàng)建下拉菜單相關(guān)實(shí)現(xiàn)技巧與操作注意事項(xiàng),需要的朋友可以參考下2019-10-10Vue 開發(fā)音樂播放器之歌手頁右側(cè)快速入口功能
這篇文章主要介紹了Vue 開發(fā)音樂播放器之歌手頁右側(cè)快速入口功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08Vue Router4中params傳參失效和報(bào)錯(cuò)問題的解決方法
在使用vue-router4中params 進(jìn)行路由組件之間傳參,跳轉(zhuǎn)頁面接收不了并報(bào)錯(cuò),本文給大家介紹了Vue Router4中params傳參失效和報(bào)錯(cuò)問題的解決方法,需要的朋友可以參考下2024-03-03Vue開發(fā)配置tsconfig.json文件的實(shí)現(xiàn)
tsconfig.json文件中指定了用來編譯這個(gè)項(xiàng)目的根文件和編譯選項(xiàng),本文就來介紹一下Vue開發(fā)配置tsconfig.json文件的實(shí)現(xiàn),感興趣的可以了解一下2023-08-08