国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > JS > 正文

用Vue.js實現(xiàn)監(jiān)聽屬性的變化

2024-05-06 16:32:54
字體:
供稿:網(wǎng)友

前言

創(chuàng)建 Vue 實例時,Vue 將遍歷 data 的屬性,通過 ES5 的 Object.defineProperty 將它們轉(zhuǎn)為 getter/setter,在其內(nèi)部 Vue 可以追蹤依賴、通知變化。

const vm = new Vue({ data: {foo: 1} // 'vm.foo' (在內(nèi)部,同 'this.foo') 是響應(yīng)的})

觀察屬性變化

Vue 的實例提供了 $watch 方法,用于觀察屬性變化。

const vm = new Vue({ data: {foo: 1}})vm.$watch('foo', function (newValue, oldValue) { console.log(newValue, oldValue) // 輸出 2 1 console.log(this.foo) // 輸出 2})vm.foo = 2

當(dāng)屬性變化后,響應(yīng)函數(shù)將會被調(diào)用,在其內(nèi)部,this 自動綁定到 Vue 的實例 vm 上。

需要注意的是,響應(yīng)是異步的。

如下:

const vm = new Vue({ data: {foo: 1}})vm.$watch('foo', function (newValue, oldValue) { console.log('inner:', newValue) // 后輸出 "inner" 2})vm.foo = 2console.log('outer:', vm.foo) // 先輸出 "outer" 2

通過 $watch Vue 實現(xiàn)了數(shù)據(jù)和視圖的綁定。觀察到數(shù)據(jù)變化,Vue 便異步更新 DOM ,在同一事件循環(huán)內(nèi),多次數(shù)據(jù)變化將會被緩存起來,在下次事件循環(huán)中,Vue 刷新隊列并僅執(zhí)行必要的更新。

如下:

const vm = new Vue({ data: {foo: 1}})vm.$watch('foo', function (newValue, oldValue) { console.log('inner:', newValue) // 后只輸出一次 "inner" 5})vm.foo = 2vm.foo = 3vm.foo = 4console.log('outer:', vm.foo) // 先輸出 "outer" 4vm.foo = 5

計算屬性

MV* 中,將 Model 層數(shù)據(jù)展現(xiàn)到 View,經(jīng)常有復(fù)雜的數(shù)據(jù)處理邏輯,這種情況下,使用計算屬性 (computed property) 更加明智。

const vm = new Vue({ data: { width: 0, height: 0, }, computed: { area () {  let output = ''  if (this.width > 0 && this.height > 0) {  const area = this.width * this.height  output = area.toFixed(2) + 'm²'  }  return output } }})vm.width = 2.34vm.height = 5.67console.log(vm.area) // 輸出 "13.27m²"

在計算屬性內(nèi)部,this 自動綁定 vm,因此聲明計算屬性時需要避免使用箭頭函數(shù)。

上例中,vm.widthvm.height 是響應(yīng)的,vm.area 內(nèi)部首次讀取 this.width this.height 時,Vue 收集其做為 vm.area 的依賴,此后 vm.width vm.height 變化時,vm.area 重新求值。計算屬性是基于它的依賴緩存,如果 vm.width vm.height 沒有變化,多次讀取 vm.area,會立即返回之前的計算結(jié)果,而不必再次求值。

同樣由于 vm.widthvm.height 是響應(yīng)的,在 vm.area 中可以將依賴的屬性賦值給一個變量,通過讀取變量來減少讀取屬性次數(shù),同時解決在條件分支中,Vue 有時會無法收集到依賴的問題。

實現(xiàn)如下:

const vm = new Vue({ data: { width: 0, height: 0, }, computed: { area () {  let output = ''  const {width, height} = this  if (width > 0 && height > 0) {  const area = width * height  output = area.toFixed(2) + 'm²'  }  return output } }})vm.width = 2.34vm.height = 5.67console.log(vm.area) // 輸出 "13.27m²"

通過 ob.js 單獨使用 Vue 的屬性觀察模塊

為方便學(xué)習(xí)和使用,ob.js 將 Vue 中屬性觀察模塊提取并封裝了一下。

ob.js GitHub 地址:https://github.com/cnlon/ob.js

安裝

npm install --save ob.js

觀察屬性變化

const target = {a: 1}ob(target, 'a', function (newValue, oldValue) { console.log(newValue, oldValue) // 3 1})target.a = 3

添加計算屬性

const target = {a: 1}ob.compute(target, 'b', function () { return this.a * 2})target.a = 10console.log(target.b) // 20

像聲明 Vue 實例一樣傳入?yún)?shù)集合

const options = { data: { PI: Math.PI, radius: 1, }, computed: { 'area': function () {  return this.PI * this.square(this.radius) }, }, watchers: { 'area': function (newValue, oldValue) {  console.log(newValue) // 28.274333882308138 }, }, methods: { square (num) {  return num * num }, },}const target = ob.react(options)target.radius = 3

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。


注:相關(guān)教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 河北省| 淮阳县| 苗栗市| 永顺县| 盈江县| 湘潭市| 任丘市| 江达县| 德安县| 长子县| 滨州市| 宾阳县| 大关县| 花莲县| 航空| 大荔县| 柘城县| 邳州市| 涞源县| 文昌市| 股票| 驻马店市| 育儿| 太原市| 南京市| 南雄市| 苍溪县| 晋州市| 昌平区| 金昌市| 金川县| 克拉玛依市| 浏阳市| 凌云县| 苏州市| 巴中市| 阿勒泰市| 金寨县| 墨竹工卡县| 寿阳县| 河北区|