什么是 behaviors
behaviors 是小程序中,用于实现组件间代码共享的特性,类似于 Vue.js 中的 “mixins”。
创建 behaviors
新建一个文件夹如:名为behaviors,在文件夹下再次创建js文件如:behaviors.js
module.exports=Behavior({
data:{
username:'love you'
},
properties:{},
methods:{
}
})
导入并使用 behaviors
在组件中,使用 require() 方法导入需要的 behavior,挂载后即可访问 behavior 中的数据或方法
const behaviors=require('../../behaviors/behaviors')
Component({
behaviors:[behaviors],
})
在wxml文件中输出
在组件的wxml文件中输出
<view>
{{username}}
</view>