微信小程序自定義組件實(shí)踐

目的

從小程序基礎(chǔ)庫版本1.6.3開始,小程序支持了簡潔的組件化編程。通過編寫一個(gè)自定義組件來學(xué)習(xí)小程序自定義組件的用法,這里我準(zhǔn)備寫一個(gè)如下的搜索框組件。


搜索框組件

流程

1.新建組件相關(guān)文件

在項(xiàng)目根目錄下新建如下文件

/component/
    searchbar/
        index.wxml
        index.js
        index.wxss
        index.json

2.編寫組件相關(guān)文件

  • index.wxml 中編寫搜索框組件的結(jié)構(gòu)(偷懶直接用了WeUI的搜索框組件改了下)
<!--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中編寫組件中綁定的幾個(gè)事件,初始化組件的屬性和數(shù)據(jù),其中用到了Compnent構(gòu)造器,相關(guān)屬性見官方文檔
    • 這里注意下getSearchValue這個(gè)方法里用到了this.triggerEvent的方法觸發(fā)一個(gè)自定義的事件search,并把搜索框內(nèi)容通過event傳輸出去,具體用法參考組件事件
//index.js 
Component({
    /**
     * 組件的屬性列表
     */
    properties: {
        title: {
            type: String,
            value: "標(biāo)題"
        },
        placeholder: {
            type: String,
            value: "搜索"
        },
        searchIconColor: {
            type: String,
            value: ""
        },
        clearIconColor: {
            type: String,
            value: ""
        }
    },
   /**
     * 組件的初始數(shù)據(jù)
     */
    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 編寫組件樣式,需要注意以下幾點(diǎn):
    • 組件和引用組件的頁面不能使用id選擇器(#a)、屬性選擇器([a])和標(biāo)簽名選擇器,請改用class選擇器。
    • 組件和引用組件的頁面中使用后代選擇器(.a .b)在一些極端情況下會(huì)有非預(yù)期的表現(xiàn),如遇,請避免使用。
    • 子元素選擇器(.a>.b)只能用于 view 組件與其子節(jié)點(diǎn)之間,用于其他組件可能導(dǎo)致非預(yù)期的情況。
    • 繼承樣式,如 font 、 color ,會(huì)從組件外繼承到組件內(nèi)。
    • 除繼承樣式外, app.wxss 中的樣式、組件所在頁面的的樣式對自定義組件無效。
/*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
{
    "component": true,
    "usingComponents": {}
}

3.使用組件

  • 先在需要使用組件的頁面的json文件中注冊組件
{
    "usingComponents": {
        "my-searchbar": "/component/searchbar/index"
    }
}
  • 在頁面wxml文件中使用組件
<my-searchbar title="標(biāo)題" placeholder="輸入搜索內(nèi)容" bind:search="onSearch" ></my-searchbar>
  • 在頁面js文件中編寫onSearch方法用于接收索框內(nèi)容
Page({
    onSearch: function(e) {
        console.log("search:", e.detail.value)
    },
})
搜索框
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容