新建component文件夹,在建个test文件夹,然后新建Component
与普通页面不同的是,这里的js中是Component({ }),从父组件接受的参数放在properties:{}中,
函数写在methods:{}中;
1、在xxx.json文件中注册:
{
"usingComponents": {
"test":"/component/test/index"
}
}
2、xxx.wxml中,使用组件
<test></test>
3、使用slot插槽
Component({
properties:{},
options:{
mutipleSlots: true,
},
data:{},
methods:{}
})
<view>
小程序<slot name="slot1"></slot>组件
<slot name="slot2"></slot>
<slot name="slot3"></slot>
</view>
<test>
<text slot="slot1">小程序</text>
<text slot="slot2">slot</text>
<text slot="slot3">组件</text>
</test>
<test prop-name="桃子">
<text slot="slot1">小程序</text>
<text slot="slot2">slot</text>
<text slot="slot3">组件</text>
</test>
Component({
properties:{
propName:{
type: String
}
},
options:{
mutipleSlots: true,
},
data:{},
methods:{}
})
<view>
小程序<slot name="slot1"></slot>组件
<slot name="slot2"></slot>
<slot name="slot3"></slot>
<text>{{propName}}</text>
</view>
<view>
小程序<slot name="slot1"></slot>组件
<slot name="slot2"></slot>
<slot name="slot3"></slot>
<text>{{propName}}</text>
<button catchtap="bindGoods">提交</button>
</view>
Component({
properties:{
propName:{
type: String
}
},
options:{
mutipleSlots: true,
},
data:{},
methods:{
bindGoods(){
const goods = {
name: '香蕉',
color: 'yellow'
}
this.triggerEvent('bindGoods', goods);
}
}
})
<test prop-name="{{name}}" bind:bindGoods="getGoods">
<text slot="slot1">小程序</text>
<text slot="slot2">slot</text>
<text slot="slot3">组件</text>
</test>
js文件
Page({
data:{
name:'桃子',
},
getGoods(e){
console.log(e);
}
})