微信小程序自定義組件components(代碼詳解)
在寫小程序代碼的時候,我們發(fā)現(xiàn)經(jīng)常有一段代碼我們經(jīng)常敲,經(jīng)常使用某一自定義組件,例如商城首頁的輪播圖和商品詳情頁的商品展示欄是近乎相同的代碼;微信小程序里的彈窗提示可以使用在多個地方…
小程序自定義組件
找到components目錄,沒有就新建

在compoents目錄里新建一個用于存放代碼的目錄(下面用g-swiper表示)
在g-swiper目錄里新建Compoent(名字自取),新建后會和新建Page時一樣自動生成四個頁面文件(g-swiper.wxml g-swiper.wxss g-swiper.js g-swiper.json)
輪播圖實例
<g-swiper list="{{imageList}}" g-class="swiper"/>
在index.wxml里只需要這簡短一行代碼就能實現(xiàn)一個輪播圖組件

json聲明
要想使用組件必先聲明,在index.json里聲明組件名稱和地址
{
"usingComponents": {
"g-swiper":"/components/g-swiper/g-swiper"
}
}
在組件的json也必須的聲明,g-swiper.json(下面代碼直接復(fù)制報錯請將注釋刪掉)
{
"component": true, // 自定義組件聲明
"usingComponents": {} // 可選項,用于引用別的組件
}
wxml和wxss
wxml和wxss里的代碼跟普通頁面里的代碼沒什么區(qū)別
g-swiper.wxml代碼
<swiper class="g-class" circular autoplay interval='3000' duration='300' indicator-dots indicator-active-color='#fffff'>
<block wx:for="{{list}}" wx:key="{{index}}">
<swiper-item class="swiper-item">
<image src="{{item}}"/>
</swiper-item>
</block>
</swiper>
g-swiper.wxss代碼
.swiper-item image{
width:100%;
height:100%
}
js
js代碼和普通頁面js代碼有所不同,這里是用Component包起來的而不是被Page包起來的
js代碼
Component({
externalClasses:["g-class"],
properties: {
list:{
type:Array,
value:[]
}
},
})
注意:這里的g-class樣式和list數(shù)據(jù)我將它的定義權(quán)利交給引入它的一方,這里是index頁面引入它
組件綁定外部方法
組件綁定外部方法的方式,以一自定義button為例
g-btn.wxml代碼
<button bindtap="btnTest">g-btn</button>
g-btn.js代碼
Component({
methods: {
/*
* 公有方法
*/
btnTest:function(){
this.triggerEvent('action')
}
}
})
在index里引入并且展示出來
index.wxml代碼
<g-btn bind:action="btnTest"></g-btn>
在index.js里加入方法btnTest()
btnTest:function(){
console.log('g-btn is clicked now!')
}
可以看到Console欄里出現(xiàn)了“g-btn is clicked now!”字樣
彈窗組件實例

index頁面引入,直接上代碼
index.wxml代碼
<view class="container"> <dialog id='dialog' title='這是標(biāo)題' content='這是對話框的內(nèi)容' cancelText='取消' confirmText='確定' bind:cancelEvent="_cancelEvent" bind:confirmEvent="_confirmEvent"> </dialog> <button type="primary" bindtap="showDialog"> ClickMe! </button> </view>
index.js代碼
Page({
onReady: function () {
//獲得dialog組件
this.dialog = this.selectComponent("#dialog");
},
showDialog() {
this.dialog.showDialog();
},
//取消事件
_cancelEvent() {
console.log('你點擊了取消');
this.dialog.hideDialog();
},
//確認(rèn)事件
_confirmEvent() {
console.log('你點擊了確定');
this.dialog.hideDialog();
}
})
組件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代碼
.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);
}
dialog.js代碼
Component({
/**
* 組件的屬性列表
* 用于組件自定義設(shè)置
*/
properties: {
// 彈窗標(biāo)題
title: { // 屬性名
type: String, // 類型(必填),目前接受的類型包括:String, Number, Boolean, Object, Array, null(表示任意類型)
value: '標(biāo)題' // 屬性初始值(可選),如果未指定則會根據(jù)類型選擇一個
},
// 彈窗內(nèi)容
content: {
type: String,
value: '彈窗內(nèi)容'
},
// 彈窗取消按鈕文字
cancelText: {
type: String,
value: '取消'
},
// 彈窗確認(rèn)按鈕文字
confirmText: {
type: String,
value: '確定'
}
},
/**
* 私有數(shù)據(jù),組件的初始數(shù)據(jù)
* 可用于模版渲染
*/
data: {
// 彈窗顯示控制
isShow: false
},
/**
* 組件的方法列表
* 更新屬性和數(shù)據(jù)的方法與更新頁面數(shù)據(jù)的方法類似
*/
methods: {
/*
* 公有方法
*/
//隱藏彈框
hideDialog() {
this.setData({
isShow: !this.data.isShow
})
},
//展示彈框
showDialog() {
this.setData({
isShow: !this.data.isShow
})
},
/*
* 內(nèi)部私有方法建議以下劃線開頭
* triggerEvent 用于觸發(fā)事件
*/
_cancelEvent() {
//觸發(fā)取消回調(diào)
this.triggerEvent("cancelEvent")
},
_confirmEvent() {
//觸發(fā)成功回調(diào)
this.triggerEvent("confirmEvent");
}
}
})
總結(jié)
以上所述是小編給大家介紹的微信小程序自定義組件components,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
- 微信小程序自定義組件實現(xiàn)多選功能
- 微信小程序自定義組件與頁面的相互傳參
- 一步步教你實現(xiàn)微信小程序自定義組件
- 微信小程序?qū)崿F(xiàn)頁面監(jiān)聽自定義組件的觸發(fā)事件
- 微信小程序頁面調(diào)用自定義組件內(nèi)的事件詳解
- 詳解微信小程序自定義組件的實現(xiàn)及數(shù)據(jù)交互
- 微信小程序自定義組件實現(xiàn)環(huán)形進度條
- 微信小程序自定義組件傳值 頁面和組件相互傳數(shù)據(jù)操作示例
- 微信小程序自定義組件的實現(xiàn)方法及自定義組件與頁面間的數(shù)據(jù)傳遞問題
- 微信小程序自定義組件封裝及父子間組件傳值的方法
- 微信小程序的自定義組件的實現(xiàn)方法
相關(guān)文章
JavaScript中的垃圾回收與內(nèi)存泄漏示例詳解
這篇文章主要給大家介紹了關(guān)于JavaScript中垃圾回收與內(nèi)存泄漏的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用JavaScript具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
重載toString實現(xiàn)JS HashMap分析
用過Java的都知道,里面有個功能強大的數(shù)據(jù)結(jié)構(gòu)——HashMap,它能提供鍵與值的對應(yīng)訪問。不過熟悉JS的朋友也會說,JS里面到處都是hashmap,因為每個對象都提供了map[key]的訪問形式。2011-03-03
關(guān)于arguments,callee,caller等的測試
關(guān)于arguments,callee,caller等的測試...2006-12-12
javascript內(nèi)置對象Math案例總結(jié)分析
今天總結(jié)一下javascript 內(nèi)置對象Math中的函數(shù)用法,順帶寫一下常見的案例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-03-03
javascript實現(xiàn)前端成語點擊驗證優(yōu)化
這篇文章主要介紹了javascript實現(xiàn)前端成語點擊驗證優(yōu)化,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-06-06

