第一步:新建my-component.js文件
導出 module.exports = Behavior({
data:{
title:"張三"
},
methods:{
fn(){
wx.showToast({
title:"我來了,你在哪?"
})
}
}
)
需要共享的數據下在data里面,需要共享的方法寫在methods里面
第二部:引入使用共享的數據和方法
const myBehavior = require("../../my-component")
引入后還需在Component中設置
Component({
behaviors:[myBehavior] //可以寫多個哦
})
這樣就可以使用共享的數據和方法了