小程序自定义组件

1、创建组件

wxml wxss js json

  • 在js中 *
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    // Component/showToast/showToast.js

    const app = getApp();
    const { requestData } = require('../../utils/util');

    Component({ //外部数据 从父组件传值
    /**
    * 组件的属性列表
    */
    properties: {
    isMask:{ // 最底层半透黑色遮罩显隐状态
    type:Boolean,
    value:false
    },
    isnoIs:{ // 不能给自己投票
    type: Boolean,
    value: false
    },
    isShut: { // 是否确认投票
    type: Boolean,
    value: false
    },
    isjorn: { // 参与过了
    type: Boolean,
    value: false
    },
    tpNameSplit: {
    type: String,
    value: ""
    },
    name: { // 被投票人姓名
    type: String,
    value: ""
    }

    },

    /**
    * 组件的初始数据
    */
    data: { //内部数据 初始化渲染

    },

    /**
    * 组件的方法列表
    */
    methods: {
    hideToast:function(){ //关闭所有弹窗 清除数据
    this.setData({
    isMask: false,
    isnoIs: false,
    isShut: false,
    isjorn: false,
    tpNameSplit: '',
    name: ''
    })
    }
    })

2、引入组件

1
2
3
4
5
6
{
"usingComponents": {
"showToast": "../../Component/showToast/showToast" //引入组件
},
"enablePullDownRefresh": true
}

3、父组件向子组件传参

在index.wxml中:

1
2
3
4
5
6
7
<showToast id='showToast'
isMask = '{{componentShowToast.isMask}}'
isnoIs='{{componentShowToast.isnoIs}}'
isShut='{{componentShowToast.isShut}}'
isjorn='{{componentShowToast.isjorn}}'
name='{{componentShowToast.name}}'
tpNameSplit='{{componentShowToast.tpNameSplit}}'></showToast>

在index.js中:

1
2
3
4
5
6
7
8
9
10
data{
componentShowToast: {
isMask: false,
isnoIs: false,
isShut: false,
isjorn: false,
tpNameSplit: '',
name:''
}
}

显而易见 我们通过子组件的properties进行通信了 (见标题1)

-------------本文结束感谢您的阅读-------------
坚持原创技术分享,您的支持将鼓励我继续创作!