為什么需要自定義組件?因為小程序通過組件來傳值,但小程序沒有提供很多傳值的方法,所以需要寫自定義組件。調用自定義組件 - 傳值給組件 - 組件傳值出來,開發者需要在組件中寫屬性及方法等。
現在來實現彈幕功能 如下:
1. 首先需要單獨創建一個組件目錄,在目錄中新建一個AuglyVideo文件夾,用來存放彈窗組件,結構如下:
2. 組件初始化准備完成。開始寫組件相關的配置。我們需要現在自定義組件聲明 component 字段為 true
//AuglyVideo.json
{
"component": true, // 自定義組件聲明
"usingComponents": {} // 可選項,用於引用別的組件
}
接下來組件的頁面結構:
//AuglyVideo.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>
//AuglyVideo.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. js文件:
Component構造器用於自定義組件,屬性、數據、方法等都是在構造器中調用
Component({
options: {
multipleSlots: true // 在組件定義時的選項中啟用多slot支持 默認只支持一個
},
/**
* 組件的屬性列表 例:頁面上的模板字符 {{title}} {{content}}
* 用於組件自定義設置
*/
properties: {
// 彈窗標題
title: { // 屬性名
type: String, // 類型(必填),可以是任意類型
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");
}
}
})
這時已經寫好了組件的屬性及方法等,之后開始組件的引用
4. 首先需要在index.json中引用它
{
"usingComponents": {
"dialog": "/Compent/AuglyVideo/AuglyVideo" //把dialog組件引用到自定義組件中
}
}
組件引用已經設置好了,接下來我們需要在index.wxml中引入它,並增加自定義的一些值
<view class="container">
<dialog id='dialog'
title='標題'
content='恭喜你,學會了小程序組件'
cancelText='知道了'
confirm='謝謝你'
bind:cancelEvent="_cancelEvent" //函數不能用雙引號括起來 例:{{_cancelEvent}}
bind:confirmEvent="_confirmEvent">
</dialog>
<button type="primary" bindtap="showDialog"> 點我 </button></view>
index.js文件
//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();
}
})
運行如下:
遇到的錯誤:
當點擊取消和確定時,發現沒有反應
解決方法:先在函數中打印下,結果發現沒有執行函數,接下來去.wxml頁面看下函數有沒有寫對,發現函數名使用雙引號括起來了。去掉雙引號就能執行
好的博客文章: