W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
為了更好定制自定義組件的功能,可以使用自定義組件擴展機制。
// behavior.js
module.exports = Behavior({
definitionFilter(defFields) {
defFields.data.from = 'behavior'
},
})
// component.js
Component({
data: {
from: 'component'
},
behaviors: [require('behavior.js')],
ready() {
console.log(this.data.from) // 此處會發(fā)現(xiàn)輸出 behavior 而不是 component
}
})
通過例子可以發(fā)現(xiàn),自定義組件的擴展其實就是提供了修改自定義組件定義段的能力,上述例子就是修改了自定義組件中的data
定義段里的內(nèi)容。
Behavior()
構造器提供了新的定義段 definitionFilter ,用于支持自定義組件擴展。 definitionFilter 是一個函數(shù),在被調(diào)用時會注入兩個參數(shù),第一個參數(shù)是使用該 behavior 的 component/behavior 的定義對象,第二個參數(shù)是該 behavior 所使用的 behavior 的 definitionFilter 函數(shù)列表。
以下舉個例子來說明:
// behavior3.js
module.exports = Behavior({
definitionFilter(defFields, definitionFilterArr) {},
})
// behavior2.js
module.exports = Behavior({
behaviors: [require('behavior3.js')],
definitionFilter(defFields, definitionFilterArr) {
// definitionFilterArr[0](defFields)
},
})
// behavior1.js
module.exports = Behavior({
behaviors: [require('behavior2.js')],
definitionFilter(defFields, definitionFilterArr) {},
})
// component.js
Component({
behaviors: [require('behavior1.js')],
})
上述代碼中聲明了1個自定義組件和3個 behavior,每個 behavior 都使用了 definitionFilter 定義段。那么按照聲明的順序會有如下事情發(fā)生:
下面利用擴展簡單實現(xiàn)自定義組件的計算屬性功能:
// behavior.js
module.exports = Behavior({
lifetimes: {
created() {
this._originalSetData = this.setData // 原始 setData
this.setData = this._setData // 封裝后的 setData
}
},
definitionFilter(defFields) {
const computed = defFields.computed || {}
const computedKeys = Object.keys(computed)
const computedCache = {}
// 計算 computed
const calcComputed = (scope, insertToData) => {
const needUpdate = {}
const data = defFields.data = defFields.data || {}
for (const key of computedKeys) {
const value = computed[key].call(scope) // 計算新值
if (computedCache[key] !== value) needUpdate[key] = computedCache[key] = value
if (insertToData) data[key] = needUpdate[key] // 直接插入到 data 中,初始化時才需要的操作
}
return needUpdate
}
// 重寫 setData 方法
defFields.methods = defFields.methods || {}
defFields.methods._setData = function (data, callback) {
const originalSetData = this._originalSetData // 原始 setData
originalSetData.call(this, data, callback) // 做 data 的 setData
const needUpdate = calcComputed(this) // 計算 computed
originalSetData.call(this, needUpdate) // 做 computed 的 setData
}
// 初始化 computed
calcComputed(defFields, true) // 計算 computed
}
})
在組件中使用:
const beh = require('./behavior.js')
Component({
behaviors: [beh],
data: {
a: 0,
},
computed: {
b() {
return this.data.a + 100
},
},
methods: {
onTap() {
this.setData({
a: ++this.data.a,
})
}
}
})
<view>data: {{a}}</view>
<view>computed: {}</view>
<button bindtap="onTap">click</button>
實現(xiàn)原理很簡單,對已有的 setData 進行二次封裝,在每次 setData 的時候計算出 computed 里各字段的值,然后設到 data 中,已達到計算屬性的效果。
此實現(xiàn)只是作為一個簡單案例來展示,請勿直接在生產(chǎn)環(huán)境中使用。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: