要点总结
properties接收父级数据this.triggerEvent('xx',yy);触发父级方法组件配置&定义
路径如下, 可以通过app.json配置
"componenets/comp1/comp1"
comp1.json 配置
{"component": true }comp1.wxml
<view class="tabs"><view class="tabs_title"><!-- tabs 接收的数据 --><!-- hanldeItemTap 接收的方法 --><viewwx:for="{{tabs}}"wx:key="id"bindtap="hanldeItemTap" /* 函数 */data-index="{{index}}" /* 数据 */>{{item.name}}</view></view><slot></slot></view>
comp1.js
Component({ properties: { //接受数据tabs tabs:{ type:Array, value:[] } }, data: { // 这里是一些组件内部数据 someData: {} }, methods: { hanldeItemTap(e){ //获取数据 const {index} = e.currentTarget.dataset; //触发 this.triggerEvent('itemChange',index); } } })page引用
test.json 注册my-comp组件
{ "usingComponents": { "my-comp":"/componenets/comp1/comp1" } }test.wxml 使用组件
//传递数据tabls 函数itemchange <Tabs tabs="{{tabs}}" binditemChange="itemChange"> //填补插槽 <block>123</block> </Tabs>test.js 触发事件
//定义事件 用于传递给组件触发 itemChange(e){ const index = e.detail; //接收数据 }
