實現小程式中的自定義元件
具體實現
要做自定義元件,我們先定一個小目標,比如說我們在小程式中實現一下 WEUI 中的彈窗元件,基本效果圖如下。
Step1
我們初始化一個小程式(本示例基礎版本庫為 1.7 ),刪掉裡面的示例程式碼,並新建一個 components
資料夾,用於存放我們以後開發中的所用元件,今天我們的目的是實現一個 彈框 元件,因此,我們在 components
元件中新建一個 Dialog
資料夾來存放我們的彈窗元件,在 Dialog
下右擊新建 Component
並命名為 dialog
後,會生成對應的 json wxml wxss js
4個檔案,也就是一個自定義元件的組成部分,此時你的專案結構應該如下圖所示:
Step2
元件初始化工作準備完成,接下來就是元件的相關配置,首先我們需要宣告自定義元件,也就是
將 dialog.json
中 component
欄位設為 true
:
{
"component": true,
"usingComponents": {}
}
其次,我們需要在 dialog.wxml
檔案中編寫彈窗元件模版,在 dialog.wxss
檔案中加入彈窗元件樣式,dialog.wxml
檔案如下:
.wx-mask {
position: fixed;
z-index: 1000;
top: 0;
right: 0;
left: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.3);
}
.wx-dialog {
position: fixed;
z-index: 5000;
width: 80%;
max-width: 600rpx;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
background-color: #fff;
text-align: center;
border-radius: 3px;
overflow: hidden;
}
.wx-dialog-title {
font-size: 18px;
padding: 15px 15px 5px;
}
.wx-dialog-content {
padding: 15px 15px 5px;
min-height: 40px;
font-size: 16px;
line-height: 1.3;
word-wrap: break-word;
word-break: break-all;
color: #999;
}
.wx-dialog-footer {
display: flex;
align-items: center;
position: relative;
line-height: 45px;
font-size: 17px;
}
.wx-dialog-footer::before {
content: '';
position: absolute;
left: 0;
top: 0;
right: 0;
height: 1px;
border-top: 1px solid #d5d5d6;
color: #d5d5d6;
-webkit-transform-origin: 0 0;
transform-origin: 0 0;
-webkit-transform: scaleY(0.5);
transform: scaleY(0.5);
}
.wx-dialog-btn {
display: block;
-webkit-flex: 1;
flex: 1;
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
position: relative;
}
.wx-dialog-footer .wx-dialog-btn:nth-of-type(1) {
color: #353535;
}
.wx-dialog-footer .wx-dialog-btn:nth-of-type(2) {
color: #3cc51f;
}
.wx-dialog-footer .wx-dialog-btn:nth-of-type(2):after {
content: " ";
position: absolute;
left: 0;
top: 0;
width: 1px;
bottom: 0;
border-left: 1px solid #d5d5d6;
color: #d5d5d6;
-webkit-transform-origin: 0 0;
transform-origin: 0 0;
-webkit-transform: scaleX(0.5);
transform: scaleX(0.5);
}
step3
元件的結構和樣式都有了,還缺少什麼呢,沒錯,還缺 js
, 眼睛比較犀利的同學,可能已經發現了我們在 dialog.wxml
檔案中的會有一些比如 {{ isShow }}
、 {{ title }}
這樣的模版變數,還定義了 _cancelEvent
和 _confirmEvent
兩個方法,其具體實現就是在 dialog.js
中。
dialog.js
是自定義元件的構造器,是使用小程式中 Component
構造器生成的,呼叫 Component
構造器時可以用來指定自定義元件的屬性、資料、方法等,具體的細節可以參考一下官方的文件
下面我通過程式碼註釋解釋一下構造器中的一些屬性的使用:
Component({
options: {
multipleSlots: true // 在元件定義時的選項中啟用多slot支援
},
/**
* 元件的屬性列表
* 用於元件自定義設定
*/
properties: {
// 彈窗標題
title: { // 屬性名
type: String, // 型別(必填),目前接受的型別包括:String, Number, Boolean, Object, Array, null(表示任意型別)
value: '標題' // 屬性初始值(可選),如果未指定則會根據型別選擇一個
},
// 彈窗內容
content: {
type: String,
value: '彈窗內容'
},
// 彈窗取消按鈕文字
cancelText: {
type: String,
value: '取消'
},
// 彈窗確認按鈕文字
confirmText: {
type: String,
value: '確定'
}
},
/**
* 私有資料,元件的初始資料
* 可用於模版渲染
*/
data: {
// 彈窗顯示控制
isShow: false
},
/**
* 元件的方法列表
* 更新屬性和資料的方法與更新頁面資料的方法類似
*/
methods: {
/*
* 公有方法
*/
//隱藏彈框
hideDialog() {
this.setData({
isShow: !this.data.isShow
})
},
//展示彈框
showDialog() {
this.setData({
isShow: !this.data.isShow
})
},
/*
* 內部私有方法建議以下劃線開頭
* triggerEvent 用於觸發事件
*/
_cancelEvent() {
//觸發取消回撥
this.triggerEvent("cancelEvent")
},
_confirmEvent() {
//觸發成功回撥
this.triggerEvent("confirmEvent");
}
}
})
step4
截至目前為止,你應該完成了一個自定義彈窗元件的大部分,可是你儲存後並沒有發現任何變化,因為我們還需要在 index.wxml
檔案中引入它!
首先需要在 index.json
中引入元件:
{
"usingComponents": {
"dialog": "/components/Dialog/dialog"
}
}
然後我們在 index.wxml
中引入它,並增加我們自定義的一些值,如下
<view class="container">
<dialog id='dialog'
title='我是標題'
content='恭喜你,學會了小程式元件'
cancelText='知道了'
confirm='謝謝你'
bind:cancelEvent="_cancelEvent"
bind:confirmEvent="_confirmEvent">
</dialog>
<button type="primary" bindtap="showDialog"> ClickMe! </button>
</view>
嗯哪,還差最後一步, index.js
配置,沒錯,這個也很簡單,我就複製貼上了
const app = getApp()
Page({
/**
* 生命週期函式--監聽頁面初次渲染完成
*/
onReady: function () {
//獲得dialog元件
this.dialog = this.selectComponent("#dialog");
},
showDialog() {
this.dialog.showDialog();
},
//取消事件
_cancelEvent() {
console.log('你點選了取消');
this.dialog.hideDialog();
},
//確認事件
_confirmEvent() {
console.log('你點選了確定');
this.dialog.hideDialog();
}
})
step5
讓我們測試一下試試看:
點選按鈕之後呢,會出現如下效果: