中文字幕一区二区人妻电影,亚洲av无码一区二区乱子伦as ,亚洲精品无码永久在线观看,亚洲成aⅴ人片久青草影院按摩,亚洲黑人巨大videos

Vue.js 監(jiān)聽屬性

本章節(jié),我們將為大家介紹 Vue.js 監(jiān)聽屬性 watch,我們可以通過 watch 來響應(yīng)數(shù)據(jù)的變化。

以下實(shí)例通過使用 watch 實(shí)現(xiàn)計(jì)數(shù)器:

實(shí)例

<div id = "app"> <p style = "font-size:25px;">計(jì)數(shù)器: {{ counter }}</p> <button @click = "counter++" style = "font-size:25px;">點(diǎn)我</button> </div> <script type = "text/javascript"> var vm = new Vue({ el: '#app', data: { counter: 1 } }); vm.$watch('counter', function(nval, oval) { alert('計(jì)數(shù)器值的變化 :' + oval + ' 變?yōu)?' + nval + '!'); }); </script>

運(yùn)行代碼 ?

以下實(shí)例進(jìn)行千米之間的換算:

實(shí)例

<div id = "computed_props"> 千米 : <input type = "text" v-model = "kilometers"> 米 : <input type = "text" v-model = "meters"> </div> <p id="info"></p> <script type = "text/javascript"> var vm = new Vue({ el: '#computed_props', data: { kilometers : 0, meters:0 }, methods: { }, computed :{ }, watch : { kilometers:function(val) { this.kilometers = val; this.meters = this.kilometers * 1000 }, meters : function (val) { this.kilometers = val/ 1000; this.meters = val; } } }); // $watch 是一個(gè)實(shí)例方法 vm.$watch('kilometers', function (newValue, oldValue) { // 這個(gè)回調(diào)將在 vm.kilometers 改變后調(diào)用 document.getElementById ("info").innerHTML = "修改前值為: " + oldValue + ",修改后值為: " + newValue; }) </script>

運(yùn)行代碼 ?

點(diǎn)擊 "運(yùn)行代碼" 按鈕查看在線實(shí)例

以上代碼中我們創(chuàng)建了兩個(gè)輸入框,data 屬性中, kilometers 和 meters 初始值都為 0。watch 對象創(chuàng)建了兩個(gè)方法 kilometers 和 meters。

當(dāng)我們再輸入框輸入數(shù)據(jù)時(shí),watch 會實(shí)時(shí)監(jiān)聽數(shù)據(jù)變化并改變自身的值??梢钥聪氯缦乱曨l演示: