赞
踩
插槽(slot)是一种用于在父组件中插入子组件内容的机制。通过使用插槽,我们可以实现组件的复用和灵活性。
在父组件中,我们可以将具体的内容放置在插槽中,并在子组件中使用插槽来展示这些内容。插槽允许父组件在渲染子组件时向其传递内容,使得子组件能够根据具体情况展示不同的内容。
以components中创建的SlotAnony 和pages中的features为例
- {
- "usingComponents": {
- "if-comp":"/components/IfComp/IfComp",
- "for-comp":"/components/ForComp/ForComp",
- "block-comp":"/components/BlockComp/BlockComp",
- "slot-anony":"/components/SlotAnony/SlotAnony"
- },
- "navigationBarTitleText": "语法特点",
- "navigationBarBackgroundColor": "#FFF",
- "navigationBarTextStyle": "black"
- }
- <view>===========条件渲染=============</view>
- <if-comp />
- <view>===========循环遍历=============</view>
- <for-comp />
- <view>===========空标签=============</view>
- <block-comp></block-comp>
- <view>===========匿名插槽=============</view>
- <slot-anony>
- <view>1111</view>
- <view>2222</view>
- </slot-anony>
- <view class="layout">
- <view>top</view>
- <slot></slot>
- <view>footer</view>
- </view>
- .layout{
- background: green;
- height: 100px;
- color: #fff;
- }
注意,当一个组件中存在多个插槽时,需要在.js文件中将multipleSlots设为true
以components中创建的SlotName 和pages中的features为例
- <view class="layout">
- <view class="left">
- <slot name="left"></slot>
- </view>
- <view class="middle">
- <slot name="middle"></slot>
- </view>
- <view class="right">
- <slot name="right"></slot>
- </view>
- </view>
- .layout{
- background: cyan;
- height: 80px;
- display: flex;
- justify-content: space-between;
- color: #fff;
- }
-
- .right{
- background: grey;
- }
- .left{
- background: red;
- }
- Component({
- options:{
- multipleSlots:true
- }
- })
- <view>===========条件渲染=============</view>
- <if-comp />
- <view>===========循环遍历=============</view>
- <for-comp />
- <view>===========空标签=============</view>
- <block-comp></block-comp>
- <view>===========匿名插槽=============</view>
- <slot-anony>
- <view>1111</view>
- <view>2222</view>
- </slot-anony>
- <view>===========具名插槽=============</view>
- <slot-name>
- <view slot="right">右侧</view>
- <view slot="left">左侧</view>
- <view slot="middle">中间</view>
- </slot-name>

- {
- "usingComponents": {
- "if-comp":"/components/IfComp/IfComp",
- "for-comp":"/components/ForComp/ForComp",
- "block-comp":"/components/BlockComp/BlockComp",
- "slot-anony":"/components/SlotAnony/SlotAnony",
- "slot-name":"/components/SlotName/SlotName"
- },
- "navigationBarTitleText": "语法特点",
- "navigationBarBackgroundColor": "#FFF",
- "navigationBarTextStyle": "black"
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。