当前位置:   article > 正文

微信小程序——插槽slot_微信小程序具名插槽

微信小程序具名插槽

插槽(slot)是一种用于在父组件中插入子组件内容的机制。通过使用插槽,我们可以实现组件的复用和灵活性。

在父组件中,我们可以将具体的内容放置在插槽中,并在子组件中使用插槽来展示这些内容。插槽允许父组件在渲染子组件时向其传递内容,使得子组件能够根据具体情况展示不同的内容。

一、匿名插槽

以components中创建的SlotAnony 和pages中的features为例

features.json

  1. {
  2. "usingComponents": {
  3. "if-comp":"/components/IfComp/IfComp",
  4. "for-comp":"/components/ForComp/ForComp",
  5. "block-comp":"/components/BlockComp/BlockComp",
  6. "slot-anony":"/components/SlotAnony/SlotAnony"
  7. },
  8. "navigationBarTitleText": "语法特点",
  9. "navigationBarBackgroundColor": "#FFF",
  10. "navigationBarTextStyle": "black"
  11. }

 features.wxml

  1. <view>===========条件渲染=============</view>
  2. <if-comp />
  3. <view>===========循环遍历=============</view>
  4. <for-comp />
  5. <view>===========空标签=============</view>
  6. <block-comp></block-comp>
  7. <view>===========匿名插槽=============</view>
  8. <slot-anony>
  9. <view>1111</view>
  10. <view>2222</view>
  11. </slot-anony>

SlotAnony.wxml

  1. <view class="layout">
  2. <view>top</view>
  3. <slot></slot>
  4. <view>footer</view>
  5. </view>

SlotAnony.wxss

  1. .layout{
  2. background: green;
  3. height: 100px;
  4. color: #fff;
  5. }

运行效果

 二、具名插槽

注意,当一个组件中存在多个插槽时,需要在.js文件中将multipleSlots设为true

以components中创建的SlotName 和pages中的features为例

 

 SlotName.wxml   (view里面有三个slot)

  1. <view class="layout">
  2. <view class="left">
  3. <slot name="left"></slot>
  4. </view>
  5. <view class="middle">
  6. <slot name="middle"></slot>
  7. </view>
  8. <view class="right">
  9. <slot name="right"></slot>
  10. </view>
  11. </view>

 SlotName.wxss

  1. .layout{
  2. background: cyan;
  3. height: 80px;
  4. display: flex;
  5. justify-content: space-between;
  6. color: #fff;
  7. }
  8. .right{
  9. background: grey;
  10. }
  11. .left{
  12. background: red;
  13. }

SlotName.js

  1. Component({
  2. options:{
  3. multipleSlots:true
  4. }
  5. })

features.wxml

  1. <view>===========条件渲染=============</view>
  2. <if-comp />
  3. <view>===========循环遍历=============</view>
  4. <for-comp />
  5. <view>===========空标签=============</view>
  6. <block-comp></block-comp>
  7. <view>===========匿名插槽=============</view>
  8. <slot-anony>
  9. <view>1111</view>
  10. <view>2222</view>
  11. </slot-anony>
  12. <view>===========具名插槽=============</view>
  13. <slot-name>
  14. <view slot="right">右侧</view>
  15. <view slot="left">左侧</view>
  16. <view slot="middle">中间</view>
  17. </slot-name>

features.json

  1. {
  2. "usingComponents": {
  3. "if-comp":"/components/IfComp/IfComp",
  4. "for-comp":"/components/ForComp/ForComp",
  5. "block-comp":"/components/BlockComp/BlockComp",
  6. "slot-anony":"/components/SlotAnony/SlotAnony",
  7. "slot-name":"/components/SlotName/SlotName"
  8. },
  9. "navigationBarTitleText": "语法特点",
  10. "navigationBarBackgroundColor": "#FFF",
  11. "navigationBarTextStyle": "black"
  12. }

运行效果

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/731105
推荐阅读
相关标签
  

闽ICP备14008679号