微信小程序自定义组件实践

目的

从小程序基础库版本1.6.3开始,小程序支持了简洁的组件化编程。通过编写一个自定义组件来学习小程序自定义组件的用法,这里我准备写一个如下的搜索框组件。

搜索框组件

流程

1.新建组件相关文件

在项目根目录下新建如下文件

1
2
3
4
5
6
/component/
searchbar/
index.wxml
index.js
index.wxss
index.json

2.编写组件相关文件

index.wxml

index.wxml 中编写搜索框组件的结构(偷懒直接用了WeUI的搜索框组件改了下)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!--index.wxml-->
<view class="my-search-bar">
<view class="my-search-bar__form">
<view class="my-search-bar__box" bindtap="showInput">
<icon class="my-icon-search_in-box" type="search" size="14" color='{{searchIconColor}}'></icon>
<input type="text" class="my-search-bar__input" placeholder="{{placeholder}}" confirm-type="search" value="{{searchValue}}" focus="{{inputFocused}}" bindinput="inputTyping" bindconfirm="getSearchValue"/>
<view class="my-icon-clear" wx:if="{{searchValue.length > 0}}" bindtap="clearInput">
<icon type="clear" size="14" color='{{clearIconColor}}'></icon>
</view>
</view>
<label class="my-search-bar__label" hidden="{{inputFocused}}" bindtap="showInput">
<icon class="my-icon-search" type="search" size="14"></icon>
<view class="my-search-bar__text">{{title}}</view>
</label>
</view>
<view class="my-search-bar__cancel-btn" hidden="{{!inputFocused}}" bindtap="hideInput">
取消
</view>
</view>

index.js

index.js中编写组件中绑定的几个事件,初始化组件的属性和数据,其中用到了Compnent构造器,相关属性见官方文档。

  • 这里注意下getSearchValue这个方法里用到了this.triggerEvent的方法触发一个自定义的事件search,并把搜索框内容通过event传输出去,具体用法参考组件事件
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
60
61
62
//index.js 
Component({
/**
* 组件的属性列表
*/
properties: {
title: {
type: String,
value: "标题"
},
placeholder: {
type: String,
value: "搜索"
},
searchIconColor: {
type: String,
value: ""
},
clearIconColor: {
type: String,
value: ""
}
},
/**
* 组件的初始数据
*/
data: {
inputFocused: false,
searchValue: ""
},
/**
* 组件的方法列表
*/
methods: {
showInput() {
this.setData({
inputFocused: true
});
},
hideInput() {
this.setData({
inputFocused: false
});
},
clearInput() {
this.setData({
searchValue: ""
});
},
inputTyping(e) {
this.setData({
searchValue: e.detail.value
});
},
getSearchValue(e) {
const eventDetails = {
value: this.data.searchValue
};
this.triggerEvent("search", eventDetails)
}
}
})

index.wxss

index.wxss 编写组件样式,需要注意以下几点:

  • 组件和引用组件的页面不能使用id选择器(#a)、属性选择器([a])和标签名选择器,请改用class选择器。
  • 组件和引用组件的页面中使用后代选择器(.a .b)在一些极端情况下会有非预期的表现,如遇,请避免使用。
  • 子元素选择器(.a>.b)只能用于 view 组件与其子节点之间,用于其他组件可能导致非预期的情况。
  • 继承样式,如 font 、 color ,会从组件外继承到组件内。
  • 除继承样式外, app.wxss 中的样式、组件所在页面的的样式对自定义组件无效。
    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
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    /*index.wxss*/
    .my-search-bar {
    position: relative;
    padding: 8px 10px;
    display: -webkit-box;
    display: -webkit-flex;
    display: flex;
    box-sizing: border-box;
    background-color: #efeff4;
    border-top: 1rpx solid #d7d6dc;
    border-bottom: 1rpx solid #d7d6dc;
    }

    .my-icon-search {
    margin-right: 8px;
    font-size: inherit;
    vertical-align: middle;
    }

    .my-icon-search_in-box {
    position: absolute;
    left: 10px;
    top: 7px;
    }

    .my-search-bar__text {
    display: inline-block;
    font-size: 14px;
    vertical-align: middle;
    }

    .my-search-bar__form {
    position: relative;
    -webkit-box-flex: 1;
    -webkit-flex: auto;
    flex: auto;
    border-radius: 5px;
    background: #fff;
    border: 1rpx solid #e6e6ea;
    }

    .my-search-bar__box {
    position: relative;
    padding-left: 30px;
    padding-right: 30px;
    width: 100%;
    box-sizing: border-box;
    z-index: 1;
    }

    .my-search-bar__input {
    height: 28px;
    line-height: 28px;
    font-size: 14px;
    }

    .my-icon-clear {
    position: absolute;
    top: 0;
    right: 0;
    padding: 7px 8px;
    font-size: 0;
    }

    .my-search-bar__label {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 2;
    border-radius: 3px;
    text-align: center;
    color: #9b9b9b;
    background: #fff;
    line-height: 28px;
    }

    .my-search-bar__cancel-btn {
    margin-left: 10px;
    line-height: 28px;
    color: #09bb07;
    white-space: nowrap;
    }
  • index.json
    1
    2
    3
    4
    {
    "component": true,
    "usingComponents": {}
    }

3.使用组件

先在需要使用组件的页面的json文件中注册组件

1
2
3
4
5
{
"usingComponents": {
"my-searchbar": "/component/searchbar/index"
}
}

在页面wxml文件中使用组件

1
<my-searchbar title="标题" placeholder="输入搜索内容" bind:search="onSearch" ></my-searchbar>

在页面js文件中编写onSearch方法用于接收索框内容

1
2
3
4
5
Page({
onSearch: function(e) {
console.log("search:", e.detail.value)
},
})

搜索框