小程序自定義組件使用
第一步:創(chuàng)建自定義組件
創(chuàng)建compoents目錄,在該目錄下創(chuàng)建要復(fù)用的組件,例如:dialog組件
其中dialo.js參數(shù)說(shuō)明:
properties:接收父級(jí)傳遞過(guò)來(lái)的值
例如:
properties: {
title:{
type:String,
value:"標(biāo)題"
},
content:{
type:String,
value:"內(nèi)容"
},
cancelText:{
type:String,
value:"取消"
},
confirmText:{
type:String,
value:"確定"
}
}
第二步:如何引入組件
例如:在login頁(yè)面中引入dialog組件
在dialog.json中引入
例如:
{
"usingComponents":{
"dialog":"/components/dialog/dialog"
}
}
然后再dialog頁(yè)面上使用dialog組件:
例如:
<dialog id="dialog"
title="這是一個(gè)標(biāo)題"
bind:confirmEvent="_queding"
bind:cancelEvent="_quxiao"
></dialog>
注意:
如何派發(fā)事件: this.triggerEvent('自定義事件名',要傳遞的數(shù)據(jù));
triggerEvent相當(dāng)于vue的$emit
例如: this.triggerEvent('confirmEvent',{name:"確定"});
如何監(jiān)聽(tīng)事件:用bind監(jiān)聽(tīng)派發(fā)過(guò)來(lái)的事件 相當(dāng)于vue中的@
例如: bind:confirmEvent="_queding"
小程序支持單slot和多slot::與vue類似
參考:https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/wxml-wxss.html
數(shù)據(jù)持久化:
cookie,H5本地存儲(chǔ)(localstrorage,sessionStorage),webSql