elasticsearch入門系列">elasticsearch入門系列
634
2022-05-30
一、Behavior
1.Behavior的定義
behaviors 是用于組件間代碼共享的特性,類似于一些編程語言中的 “mixins” 或 “traits”。
每個 behavior 可以包含一組屬性、數(shù)據(jù)、生命周期函數(shù)和方法。組件引用它時,它的屬性、數(shù)據(jù)和方法會被合并到組件中,生命周期函數(shù)也會在對應(yīng)時機(jī)被調(diào)用。 每個組件可以引用多個 behavior ,behavior 也可以引用其它 behavior 。
2.Behavior的屬性
3.使用
module.exports = Behavior({ behaviors: [], properties: { myBehaviorProperty: { type: String } }, data: { myBehaviorData: {} }, attached: function(){}, methods: { myBehaviorMethod: function(){} } })
// my-component.js var myBehavior = require('my-behavior') Component({ behaviors: [myBehavior], properties: { myProperty: { type: String } }, data: { myData: 'my-component-data' }, created: function () { console.log('[my-component] created') }, attached: function () { console.log('[my-component] attached') }, ready: function () { console.log('[my-component] ready') }, methods: { myMethod: function () { console.log('[my-component] log by myMethod') }, } })
注意點:Behavior創(chuàng)建事件會早于組件,所以組件的同名屬性會覆蓋Behavior
二、內(nèi)置的Behavior
1.wx://form-field
使自定義組件有類似于表單控件的行為。 form 組件可以識別這些自定義組件,并在 submit 事件中返回組件的字段名及其對應(yīng)字段值。
2.wx://form-field-group
使 form 組件可以識別到這個自定義組件內(nèi)部的所有表單控件。
3.wx://form-field-button
使 form 組件可以識別到這個自定義組件內(nèi)部的 button 。如果自定義組件內(nèi)部有設(shè)置了 form-type 的 button ,它將被組件外的 form 接受。
4.wx://component-export
使自定義組件支持 export 定義段。這個定義段可以用于指定組件被 selectComponent 調(diào)用時的返回值。
小程序
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。