自定义组件
组件的样式
样式的隔离
- isolated:相互隔离
- shared父子:相互影响
- apply-shared:父影响子
外部类
externalClasses:["cell-class"],
<view class="cell cell-class">我是cell组件</view>
<cell cell-class="mycell"></cell>
<cell></cell>
.mycell{
line-height: 120rpx !important;
color: #f70;
}
组件的插槽
默认插槽
父组件:<cell><text>工资1w</text></cell>
子组件:<view><slot></slot></view>
命名多插槽
父组件:<cell><text slot="next">💓</text><text slot="pre">💟</text></cell>
子组件:<view class="cell cell-class"><slot name="pre"></slot><slot name="next"></slot></view>
组件的传参
- 父传子:property
- 子传参父:triggerEvent
定义基础的三个组件
定义组件item
props
- title:标题
- icon:图标
- tip:提示
- url:跳转链接
- open-type:打开的方式
properties: {
title:{
type:String,
value:""
},
showSlot:{
type:Boolean,
value:false
},
icon:{
type:String,
value:""
},
tip:{
type:String,
value:""
},
badge:{
type:[Boolean,Number],
value:false
},
url:{
type:String,
value:""
},
openType:{
type:String,
value:"navigate"
}
},
slot:插槽
<navigator class="item itemclass" url="{{url}}" open-type="{{openType}}" bindtap="itemclick">
<view class="icon" wx:if="{{icon}}">
<image src="{{icon}}" mode="aspectFill"/>
</view>
<view class="content">
<view class="title" wx:if="{{title}}">{{title}}</view>
<slot name="title" wx:else></slot>
<view class="right" wx:if="{{!showSlot}}">
<view class="tip">{{tip}}</view>
<view class="badge" wx:if="{{badge}}">
<view wx:if="{{badge==true}}" class="dot"></view>
<view wx:else class="redbadeg">{{badge}}</view>
</view>
<view class="arrow"></view>
</view>
<slot name="right" wx:else></slot>
</view>
</navigator>
event:事件
外部类
externalClasses:["itemclass"],
定义组件component
- 生命周期:lifetimes(attach组件的挂载)
- data:数据
- methods:方法
- properties:属性(只读)
- externalClass:外部类
- options:选项
1、multipleSlots:开启多个插槽
2、stylelsolation:“isolated”:组件的样式格式
定义导航栏
- wx.getMenuButtonBoundingClientRect:胶囊的边界
const res = wx.getMenuButtonBoundingClientRect()
console.log(res);
this.setData({titleWidth:res.left-7})
var barHeight = res.height+(res.top-info.statusBarHeight)*2
this.setData({barHeight})
- wx.getSystemInfoSync:系统信息(获取状态栏的高度)
var info = wx.getSystemInfoSync()
console.log(info);
this.setData({statusBarHeight:info.statusBarHeight})
{
"usingComponents": {
"nav":"/components/nav/nav"
},
"navigationStyle": "custom"
}
lifetimes:{
attached(){
var info = wx.getSystemInfoSync()
console.log(info);
this.setData({statusBarHeight:info.statusBarHeight})
const res = wx.getMenuButtonBoundingClientRect()
console.log(res);
this.setData({titleWidth:res.left-7})
var barHeight = res.height+(res.top-info.statusBarHeight)*2
this.setData({barHeight})
var pages = getCurrentPages();
console.log(pages);
this.setData({pagesLen:pages.length})
}
},
组件的使用
定义组件
页面的xxx.json usingComponent注册
- “usingComponents”: {“cell”:“/components/cell/cell”,“item”:“/components/item/item”}
使用组件
- < cell>< /cell>
- < item>< /item>
自定义组件的生命周期
组件的生命周期(lifetimes)
- created:创建(此时还不能调用setData)
- attach:挂载
- detached:卸载
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/df1ed2d91de3d8e7723679db4a3c5e6f.png)
页面的生命周期(pageLifetimes)
- show:显示
- hide:后台运行
- resize:尺寸变化