• 微信小程序自定义组件、组件应用、插槽、slot


    一、自定义组件

    官方文档:自定义组件 | 微信开放文档

    二、组件部分

    1、在component下创建组件文件夹 testComponent

     2、启用组件 test_component.json( 创建文件时自动就是true就不用在改了component: true,)

    1. {
    2. "component": true,
    3. }

     3、test_component.wxml

    1. <view class="test-title" wx:if="{{isShow}}" wx:for="{{listData}}" wx:key="index">
    2. <view bindtap="clickFun" data-item="{{item}}" class="test-item">{{index}}、{{item}view>
    3. view>

    3、test_component.js

    1. Component({
    2. /**
    3. * 组件的属性列表
    4. */
    5. properties: {
    6. isShow:{//设置isShow 类型与值
    7. type:Boolean,
    8. value:[]
    9. },
    10. listData:{//设置listData 类型
    11. type:Array,
    12. value:[]
    13. }
    14. },
    15. /**
    16. * 组件的初始数据
    17. */
    18. data: {},
    19. /**
    20. * 组件的方法列表
    21. */
    22. methods: {
    23. clickFun(e){//一些要处理的逻辑
    24. console.log(e.currentTarget.dataset.item)
    25. },
    26. }
    27. })

     三、调用组件

    这边在index页面调用

    1、index.json 引用组件 test_componet

    (可以引用单个或多个,globalLoader-cmp可以不用管是我这里需要的)

    1. {
    2. "navigationBarTitleText": "test",
    3. "backgroundColor": "#ffffff",
    4. "usingComponents": {
    5. "test_component": "/component/testComponent/test_component",
    6. "globalLoader-cmp": "/component/globalLoader/globalLoader"
    7. }

    2、index.wxml

    (这里isShow="{{isShow}}" 第一个是对应组件页面里wx:if="{{isShow}}" 里的 ;第二个可以自己随便起名字,是对应自己index.js里data的数据字段,listData和他一样)

    <test_component isShow="{{isShow}}" listData="{{listData}}">test_component>
    

    3、index.js  data里设置需要的值

    1. data: {
    2. isShow:true,
    3. listData:['测试一','abc','描述'],
    4. }

    自定义组件——插槽 slot 

    官方文档 组件模板和样式 | 微信开放文档

    默认只有一个插槽,也可设置多插槽,通俗点就是插入到组件里的内容

    单插槽的时候直接插入组件就可以

    1. <test_component isShow="{{isShow}}" listData="{{listData}}">
    2. <view>我是单插槽插入的内容view>
    3. test_component>

    如果用多插槽,需要在组件test_component.js里设置启用,

    1. <test_component isShow="{{isShow}}" listData="{{listData}}">
    2. <slot name="before">我是插槽一的数据slot>
    3. <slot name="after">我是插槽二的数据slot>
    4. test_component>
    1. Component({
    2. options:{
    3. multipleSlots:true//开启多插槽
    4. },
    5. /**
    6. * 组件的属性列表
    7. */
    8. properties: {
    9. isShow:{
    10. type:Boolean,
    11. value:[]
    12. },
    13. listData:{
    14. type:Array,
    15. value:[]
    16. }
    17. },
    18. /**
    19. * 组件的初始数据
    20. */
    21. data: {},
    22. /**
    23. * 组件的方法列表
    24. */
    25. methods: {
    26. clickFun(e){//要处理的逻辑
    27. console.log(e.currentTarget.dataset.item)
    28. },
    29. }
    30. })

     当多插槽时,需要通过name来指定具体插槽

    1. "{{isShow}}" listData="{{listData}}">
    2. <view slot="before">我是插槽一的数据view>
    3. <view slot="after">我是插槽二的数据view>

    展示结果

  • 相关阅读:
    java题目 ----选择结构
    Java基础题目
    前端稳定性建设
    自定义数据类型:结构体、枚举、联合
    MySQL--MySQL表的增删改查(进阶)
    SpringBoot 2.6. 整合springfox 3.0报错问题解决
    堪称完美,仅用了330页直接封神,被大家吹爆的RocketMQ笔记
    浅析Java设计模式【3.6】——模板方法
    单目标追踪——【Transformer】Learning Spatio-Temporal Transformer for Visual Tracking
    2014年下半年 系统架构设计师 下午论文
  • 原文地址:https://blog.csdn.net/qq_39109182/article/details/126290457