赞
踩
<cell title="今天吃大餐">我是父组件</cell>
<view>{{title}}</view> //今天吃大餐
properties: { // 标题 title:{ type:String, value:"" }, // 显示右侧插槽 showrslot:{ 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" } },
父组件向子组件传递值的时候是通过properties属性的,在item组件的js文件写法跟平时有些不同,我们会将所有的配置对象,例如data,methods,properties,都写在options方法中
<view class="cell" bindtap="tapHd">
<text>{{title}}</text>
<text>{{count}}</text>
</view>
// components/cell/cell.js Component({ // 选项: options:{ // 样式隔离:apply-shared 父影响子,shared父子相互影响, isolated相互隔离 styleIsolation:"isolated", // 允许多个插槽 multipleSlots:true, }, // 通过组件的外部类实现父组件控制子自己的样式 externalClasses:["cell-class"], /** * 组件的属性列表 */ properties: { title:{ type:String, value:"" }, num:{ // 定了类型 type:Number, //定义默认值 value:1 } }, /** * 组件的初始数据 */ data: { count:1 }, lifetimes:{ // 当组件的生命周期被挂载时候 attached(){ console.log(this.data) // cout的值为父组件传递的num值 this.setData({count:this.data.num}) } }, /** * 组件的方法列表 */ methods: { tapHd(){ this.setData({count:this.data.count+1}) // 发送一个事件 this.triggerEvent("cellclick",this.data.count) } } })
<cell bind:cellclick="clickHd"></cell>
// components/cell/cell.js Component({ // 选项: options:{ // 样式隔离:apply-shared 父影响子,shared父子相互影响, isolated相互隔离 styleIsolation:"isolated", // 允许多个插槽 multipleSlots:true, }, // 通过组件的外部类实现父组件控制子自己的样式 externalClasses:["cell-class"], /** * 组件的属性列表 */ properties: { title:{ type:String, value:"" }, num:{ // 定了类型 type:Number, //定义默认值 value:1 } }, /** * 组件的初始数据 */ data: { count:1 }, lifetimes:{ // 当组件的生命周期被挂载时候 attached(){ console.log(this.data) // cout的值为父组件传递的num值 this.setData({count:this.data.num}) } }, /** * 组件的方法列表 */ methods: { tapHd(){ this.setData({count:this.data.count+1}) // 发送一个事件 this.triggerEvent("cellclick",this.data.count) } } })
options:{
// 样式隔离:apply-shared 父影响子,shared父子相互影响, isolated相互隔离
styleIsolation:"isolated",
// 允许多个插槽
multipleSlots:true,
},
<view cell-class='mycell1'>
我是cell组件
</view>
<view cell-class='mycell2'>
我是cell组件
</view>
.mycell1{
color: #f70;
background-color: seagreen;
line-height: 88rpx;
}
.mycell2{
color: 'black';
background-color: sienna;
line-height: 88rpx;
}
这样我们就能够分别定义组件的样式了
<cell>我在这里</cell>
<view>父组件插件来的内容都放在slot里面
<slot></slot>
<view>
<cell cell-class="mycell">
<text>胡萝卜</text>
<text slot="first">第一</text>
<text slot="second">第二</text>
</cell>
<view>
<slot name="first"></slot>
<slot name="second"></slot>
</view>
<navigator class="item itemclass" url="{{url}}" open-type="{{openType}}" bindtap="itemclick"> <view class="icon" wx:if="{{icon}}"> <image src="{{icon}}" mode="aspectFill"></image> </view> <view class="content"> <view class="title"> <view class="title" wx:if="{{title}}">{{title}}</view> <slot name="title" wx:else ></slot> </view> <view class="right" wx:if="{{!showrslot}}"> <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> <!-- icon 图标 conent 内容 title 标题 right 右侧 tip 提示 badge 红点 arrow 箭头 -->
item.js文件
Component({ options:{ multipleSlots:true }, externalClasses:["itemclass"], /** * 组件的属性列表 */ properties: { // 标题 title:{ type:String, value:"" }, // 显示右侧插槽 showrslot:{ 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" } }, /** * 组件的初始数据 */ data: { }, /** * 组件的方法列表 */ methods: { itemclick(e){ console.log(e); //发送一个事件 this.triggerEvent("itemclick",e.detail) } } })
item.wxss文件
/* components/item/item.wxss */ .item{ line-height: 88rpx; display: flex; align-items: center; justify-content: space-between; } .icon{ margin-left: 30rpx; margin-right: 30rpx; height: 100%; display: flex; align-items: center; } .icon image{ width: 60rpx; height: 60rpx; } .content{ padding: 0 30rpx; border-bottom: 1rpx solid #ccc; display: flex; flex:1; } .title{ flex:1; color:#333; font-size: 32rpx; } .right{ display: flex; align-items: center; } .right .arrow{ height: 30rpx; width: 30rpx; border-top:3rpx solid #999; border-right: 3rpx solid #999; transform: rotate(45deg); } .tip{ color:#999; font-size: 24rpx; } .dot{ height: 14rpx; width: 14rpx; background-color: #f00; border-radius: 12rpx; margin-left: 15rpx; } .redbadeg{ font-size: 18rpx; color:#fff; border-radius: 18rpx; background-color: #f00; max-height: 30rpx; width: 30rpx; line-height: 30rpx; text-align: center; margin-left: 15rpx; }
<item title="支付" icon="/images/icon01.png"></item>
<item title="相册" icon="/images/icon02.png"></item>
<item title="支付" ></item>
<item title="消息" icon="/images/icon02.png" badge="{{true}}" tip="3条未读"></item>
<item title="消息" icon="/images/icon02.png" badge="{{12}}" tip="12条未读"></item>
<item title="消息" icon="/images/icon02.png" showrslot="{{true}}">
<switch checked="true" slot="right"></switch>
</item>
<item>
<view slot="title">插槽title</view>
</item>
<item title="新闻" icon="/images/icon02.png" url="/pages/yidian/yidian" open-type="switchTab">
</item>
<item title="首页" icon="/images/icon02.png" url="/pages/home/home" >
</item>
<item title="消息" icon="/images/icon02.png" showrslot="{{true}}" itemclass="myitem">
<switch checked="true" slot="right"></switch>
</item>
<!-- <view class="title">自定义组件</view>
<cell bind:cellclick="cellHd" title="中美两国和平相处" num="{{5}}"></cell>
<cell bind:cellclick="cellHd" title="阶级不同立场也不同" ></cell> -->
<!-- <cell cell-class="mycell">
<text slot="pre">声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/437393
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。