一、新建conponents文件
新建一個 components 文件夾,用於存放我們以后開發中的所用組件,例如目的是實現一個 彈框 組件,因此,在 components 組件中新建一個 Dialog 文件夾來存放彈窗組件,在 Dialog 下右擊新建 Component 並命名為 dialog 后,會生成對應的 json wxml wxss js 4個文件,也就是一個自定義組件的組成部分,此時項目結構應該如下圖所示:
二、組件的相關配置及寫法
1.需要聲明自定義組件,也就是將 dialog.json 中 component 字段設為 true
{
"component": true, // 自定義組件聲明
"usingComponents": {} // 可選項,用於引用別的組件
}
2.需要在 dialog.wxml 文件中編寫彈窗組件模版,在 dialog.wxss 文件中加入彈窗組件樣式,它們的寫法與頁面的寫法類似
dialog.wxml 文件:
<!--component/dialog/dialog.wxml-->
<view class='wx_dialog_container' hidden="{{!isShow}}">
<view class='wx-mask'></view>
<view class='wx-dialog'>
<view class='wx-dialog-title'>{{ title }}</view>
<view class='wx-dialog-content'>{{ content }}</view>
<view class='wx-dialog-footer'>
<view class='wx-dialog-btn' catchtap='_cancelEvent'>{{ cancelText }}</view>
<view class='wx-dialog-btn' catchtap='_confirmEvent'>{{ confirmText }}</view>
</view>
</view>
</view>
dialog.wxss 文件:
/* components/Dialog/dialog.wxss */
.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: #FFFFFF;
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: #999999;
}
.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);
}
3.dialog.js文件
dialog.js 是自定義組件的構造器,是使用小程序中 Component 構造器生成的,調用 Component 構造器時可以用來指定自定義組件的屬性、數據、方法等,具體的細節可以參考一下官方的文檔
下面通過代碼注釋解釋一下構造器中的一些屬性的使用:
// components/Dialog/dialog.js
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");
}
}
})
如果方法需要傳遞參數,則:
三、組件的使用
完成了一個自定義彈窗組件的大部分,可是保存后並沒有發現任何變化,因為還需要在 dialogDemo.wxml 文件中引入它!
首先需要在 dialogDemo.json 中引入組件
{
"usingComponents": {
"dialog": "../../components/Dialog/dialog"
}
}
然后在 dialogDemo.wxml 中引入它,並增加自定義的一些值,如下:
<!--pages/dialogDemo/dialogDemo.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>
最后一步, dialogDemo.js 配置
// pages/dialogDemo/dialogDemo.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();
}
})