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

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

json聲明
要想使用組件必先聲明,在index.json里聲明組件名稱和地址
{
"usingComponents": {
"g-swiper":"/components/g-swiper/g-swiper"
}
}
在組件的json也必須的聲明,g-swiper.json(下面代碼直接復(fù)制報(bào)錯(cuò)請(qǐng)將注釋刪掉)
{
"component": true, // 自定義組件聲明
"usingComponents": {} // 可選項(xiàng),用于引用別的組件
}
wxml和wxss
wxml和wxss里的代碼跟普通頁(yè)面里的代碼沒(méi)什么區(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代碼和普通頁(yè)面js代碼有所不同,這里是用Component包起來(lái)的而不是被Page包起來(lái)的
js代碼
Component({
externalClasses:["g-class"],
properties: {
list:{
type:Array,
value:[]
}
},
})
注意:這里的g-class樣式和list數(shù)據(jù)我將它的定義權(quán)利交給引入它的一方,這里是index頁(yè)面引入它
組件綁定外部方法
組件綁定外部方法的方式,以一自定義button為例
g-btn.wxml代碼
<button bindtap="btnTest">g-btn</button>
g-btn.js代碼
Component({
methods: {
/*
* 公有方法
*/
btnTest:function(){
this.triggerEvent('action')
}
}
})
在index里引入并且展示出來(lái)
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!”字樣
彈窗組件實(shí)例

index頁(yè)面引入,直接上代碼
index.wxml代碼
<view class="container"> <dialog id='dialog' title='這是標(biāo)題' content='這是對(duì)話框的內(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('你點(diǎn)擊了取消');
this.dialog.hideDialog();
},
//確認(rèn)事件
_confirmEvent() {
console.log('你點(diǎn)擊了確定');
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)題' // 屬性初始值(可選),如果未指定則會(huì)根據(jù)類型選擇一個(gè)
},
// 彈窗內(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ù)的方法與更新頁(yè)面數(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,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
- 微信小程序自定義組件實(shí)現(xiàn)多選功能
- 微信小程序自定義組件與頁(yè)面的相互傳參
- 一步步教你實(shí)現(xiàn)微信小程序自定義組件
- 微信小程序?qū)崿F(xiàn)頁(yè)面監(jiān)聽(tīng)自定義組件的觸發(fā)事件
- 微信小程序頁(yè)面調(diào)用自定義組件內(nèi)的事件詳解
- 詳解微信小程序自定義組件的實(shí)現(xiàn)及數(shù)據(jù)交互
- 微信小程序自定義組件實(shí)現(xiàn)環(huán)形進(jìn)度條
- 微信小程序自定義組件傳值 頁(yè)面和組件相互傳數(shù)據(jù)操作示例
- 微信小程序自定義組件的實(shí)現(xiàn)方法及自定義組件與頁(yè)面間的數(shù)據(jù)傳遞問(wèn)題
- 微信小程序自定義組件封裝及父子間組件傳值的方法
- 微信小程序的自定義組件的實(shí)現(xiàn)方法
相關(guān)文章
JavaScript中的垃圾回收與內(nèi)存泄漏示例詳解
這篇文章主要給大家介紹了關(guān)于JavaScript中垃圾回收與內(nèi)存泄漏的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用JavaScript具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
重載toString實(shí)現(xiàn)JS HashMap分析
用過(guò)Java的都知道,里面有個(gè)功能強(qiáng)大的數(shù)據(jù)結(jié)構(gòu)——HashMap,它能提供鍵與值的對(duì)應(yīng)訪問(wèn)。不過(guò)熟悉JS的朋友也會(huì)說(shuō),JS里面到處都是hashmap,因?yàn)槊總€(gè)對(duì)象都提供了map[key]的訪問(wèn)形式。2011-03-03
關(guān)于arguments,callee,caller等的測(cè)試
關(guān)于arguments,callee,caller等的測(cè)試...2006-12-12
javascript內(nèi)置對(duì)象Math案例總結(jié)分析
今天總結(jié)一下javascript 內(nèi)置對(duì)象Math中的函數(shù)用法,順帶寫一下常見(jiàn)的案例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
javascript實(shí)現(xiàn)前端成語(yǔ)點(diǎn)擊驗(yàn)證優(yōu)化
這篇文章主要介紹了javascript實(shí)現(xiàn)前端成語(yǔ)點(diǎn)擊驗(yàn)證優(yōu)化,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
js對(duì)字符串進(jìn)行編碼的方法總結(jié)(推薦)
下面小編就為大家?guī)?lái)一篇js對(duì)字符串進(jìn)行編碼的方法總結(jié)(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11

