11. slot插槽

slot标签其实就是一个占位符 插槽

等到父组件调用子组件的时候再传递标签过来,最终这些被传递的标签就会替换slot插槽的位置

子组件页面:

<!--components/Tabs.wxml-->
<view class="tabs">
    <view class="tabs_title">
        <view
             wx:for="{{tabs}}"
             wx:key="id"
             data-index="{{index}}"
             class="title_item {{item.isActive?'active':''}}"
             bindtap="handleItemTap">{{item.name}}</view>
    </view>
    <view class="tabs_content">
        <slot></slot>
    </view>
</view>

子组件js

// pages/demoComponent/demoComponent.js
Page({
  data: {
    tabs: [
      {
        id: 1,
        name: '首页',
        isActive: true
      }, {
        id: 2,
        name: '原创',
        isActive: false
      }, {
        id: 3,
        name: '分类',
        isActive: false
      }, {
        id: 4,
        name: '关于',
        isActive: false
      }
    ]
  },
  handleItemChange(e){
    console.log('handleItemChange')
    console.log(e)
    console.log(e.detail.index)
    const {index} = e.detail;
    let {tabs} = this.data;
    tabs.forEach((v,i)=> {
      i === index ? v.isActive = true : v.isActive = false;
    })

    this.setData({
      tabs
    })
  }
})

父组件json

{
  "usingComponents": {
    "Tabs": "../../components/Tabs/Tabs"
  }
}

父组件js

// pages/demoComponent/demoComponent.js
Page({
  data: {
    tabs: [
      {
        id: 1,
        name: '首页',
        isActive: true
      }, {
        id: 2,
        name: '原创',
        isActive: false
      }, {
        id: 3,
        name: '分类',
        isActive: false
      }, {
        id: 4,
        name: '关于',
        isActive: false
      }
    ]
  },
  handleItemChange(e){
    console.log('handleItemChange')
    console.log(e)
    console.log(e.detail.index)
    const {index} = e.detail;
    let {tabs} = this.data;
    tabs.forEach((v,i)=> {
      i === index ? v.isActive = true : v.isActive = false;
    })

    this.setData({
      tabs
    })
  }
})

父组件页面

<!--pages/demoComponent/demoComponent.wxml-->
<view>pages/demoComponent/demoComponent.wxml</view>
<view>1.父组件(页面)向子组件传递数据通过标签属性的方式来传递</view>
<Tabs tabs="{{tabs}}" binditemChange="handleItemChange">
    <view hidden="{{tabs[0].isActive?false:true}}">这里是父组件往子组件插槽中传递的内容1</view>
    <view hidden="{{tabs[1].isActive?false:true}}">这里是父组件往子组件插槽中传递的内容2</view>
    <view hidden="{{tabs[2].isActive?false:true}}">这里是父组件往子组件插槽中传递的内容3</view>
    <view hidden="{{tabs[3].isActive?false:true}}">这里是父组件往子组件插槽中传递的内容4</view>
</Tabs>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值