properties: {
icon:{
type:String,
value:''
}
}
properties: {
icon:String
}
properties: {
icon: {
type: String,
value: 'icon-cart-empty'
},
title: {
type: String,
value: '购物车'
}
}
<view class="empty-com">
<text class="iconfont {{icon}}"></text>
<text>{{title}}空空的,去逛逛吧</text>
</view>
Component({
properties: {
h:{
type:Number,
value: 20
},
imgs: {
type: Array,
value: []
}
}
})
<swiper class="swiper" style="height: {{h}}vh;" indicator-dots indicator-color="#fff" indicator-active-color="#f40" autoplay circular>
<swiper-item wx:for='{{imgs}}' wx:key='id'>
<image class="img" src="{{item.url}}" mode="aspectFill"></image>
</swiper-item>
</swiper>
.img{
width: 100%;
}
{
"usingComponents": {
"my-swiper": "../../components/my-swiper/my-swiper"
}
}
<my-swiper h='{{25}}' imgs='{{imgs}}'></m-swiper>
wx.request({
url: 'https://glpla.github.io/utils/data/swiper.json',
success: res => {
console.log('res', res);
this.setData({
imgs: res.data.cont
})
}
})
右键单击组件根目录 components → 新建组件目录 my-iconfont
再右键组件目录,新建 Component 为 my-iconfont;组件名和组件目录通常保持一致
wxml:基类 iconfont 保留,不同的图标类 icon 通过父组件传递获取
<text class="iconfont {{icon}}"></text>
js:在属性 properties 中设置将图标类 icon 的类型和默认值;并允许添加全局类
Component({
options: {
addGlobalClass: true,
},
properties: {
icon:{
type:String
}
//简化形式icon:String
}
})
在使用组件的页面 .json 中配置
{
"usingComponents": {
"my-iconfont":"../../components/my-iconfont/my-iconfont"
}
}
父组件 - 使用组件
在使用组件的 .wxml 中,将图标类作为属性指定给参数接口即可
<my-iconfont icon='icon-home'></my-iconfont>
子组件wxml - 声明普通事件tap并指定事件处理函数toCalendar
<view class='nav-item' bindtap="toCalendar">navigation</view>
子组件js - 在事件处理函数 toCalendar 中触发自定义事件 toCal
toCalendar() {
this.triggerEvent('toCal');
}
父组件 - 使用组件 xml - 绑定子组件发送的事件toCal并指定事件处理函数 dealCal
<nav bindtoCal="dealCal"></nav>
父组件 - 使用组件js - 实现事件处理函数dealCal
dealCal() {
wx.navigateTo({
url: '../calendar/calendar',
})
}