微信小程序自定義prompt組件步驟詳解
步驟一:新建一個(gè)component的文件夾,用來(lái)放所有的自定義組件;
步驟二:在該目錄下新建一個(gè)prompt的文件夾,用來(lái)放prompt組件;
步驟三:右擊–>新建–>component

直接上代碼
wxml
<view class="prompt-box" hidden="{{isHidden}}">
<view class="prompt-content contentFontColor">
<view class="prompt-title">{{title}}</view>
<input class="prompt-input" type="digit" bindinput="_input" value="{{cost}}" />
<view class="prompt-btn-group">
<button class="btn-item prompt-cancel-btn contentFontColor" bind:tap="_cancel">{{btn_cancel}}</button>
<button class="btn-item prompt-certain-btn" bind:tap="_confirm">{{btn_certain}}</button>
</view>
</view>
</view>
js
// components/prompt/prompt.js
Component({
options: {
multipleSlots: true // 在組件定義時(shí)的選項(xiàng)中啟用多slot支持
},
/**
* 組件的屬性列表
*/
properties: {
title: {
type: String,
value: '標(biāo)題'
},
btn_cancel: {
type: String,
value: '取消'
},
btn_certain: {
type: String,
value: '確定'
}
},
data: {
isHidden: true,
},
methods: {
hidePrompt: function () {
this.setData({
isHidden: true
})
},
showPrompt () {
this.setData({
isHidden: false
})
},
/*
* 內(nèi)部私有方法建議以下劃線開(kāi)頭
* triggerEvent 用于觸發(fā)事件
*/
_cancel () {
//觸發(fā)cancel事件,即在外部,在組件上綁定cancel事件即可,bind:cancel,像綁定tap一樣
this.triggerEvent("cancel")
},
_confirm () {
this.triggerEvent("confirm");
},
_input(e){
//將參數(shù)傳出去,這樣在getInput函數(shù)中可以通過(guò)e去獲得必要的參數(shù)
this.triggerEvent("getInput",e.detail);
}
}
})
json
{
"component": true,
"usingComponents": {}
}
wxss
/* components/vas-prompt/vas-prompt.wxss */
.prompt-box {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
z-index: 11;
background: rgba(0, 0, 0, .5);
}
.prompt-content {
position: absolute;
left: 50%;
top: 40%;
width: 80%;
max-width: 600rpx;
border: 2rpx solid #ccc;
border-radius: 10rpx;
box-sizing: bordre-box;
transform: translate(-50%, -50%);
overflow: hidden;
background: #fff;
}
.prompt-title {
width: 100%;
padding: 20rpx;
text-align: center;
font-size: 40rpx;
border-bottom: 2rpx solid gray;
}
.prompt-input{
margin: 8%;
padding: 10rpx 15rpx;
width: 80%;
height:85rpx;
border: 1px solid #ccc;
border-radius: 10rpx;
}
.prompt-btn-group{
display: flex;
}
.btn-item {
width: 35%;
margin-bottom: 20rpx;
height: 100rpx;
line-height: 100rpx;
background-color: white;
justify-content: space-around;
}
.prompt-certain-btn{
color: white;
background-color: #4FEBDE;
}
.prompt-cancel-btn{
border: 1px solid #4FEBDE;
}
.contentFontColor {
color: #868686;
}
使用
例如,在index.html中使用
在json中添加useComponents屬性
"usingComponents": {
"vas-prompt": "./components/prompt/prompt"
}
wxml
<prompt id="prompt" title='test' btn_certain='確定' bind:getInput="getInput" bind:cancel="cancel" bind:confirm="confirm"> </prompt> <button bindtap="showPrompt">點(diǎn)擊彈出prompt</button>
js
//在onReady生命周期函數(shù)中,先獲取prompt實(shí)例
onReady:function(){
this.prompt = this.selectComponent("#prompt");
},
//顯示prompt
showPrompt:function(){
this.prompt.showPrompt();
},
//將輸入的value保存起來(lái)
getInput: function (e) {
this.setData({
value: e.detail.value
})
},
confirm: function () {
let _cost = this.data.value;
if (_cost == '') {
console.log('你還未輸入');
return;
}
else{
....
}
},
cancel: function () {
this.prompt.hidePrompt();
},
原理:
將prompt隱藏起來(lái),點(diǎn)擊顯示的時(shí)候則顯示,然后通過(guò)原生的tap事件,觸發(fā)自定義事件,在使用該組件的時(shí)候,則使用自定義事件.
總結(jié)
以上所述是小編給大家介紹的微信小程序自定義prompt組件步驟詳解,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
JavaScript如何實(shí)現(xiàn)圖片處理與合成
這篇文章主要介紹了JavaScript如何實(shí)現(xiàn)圖片處理與合成,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
javascript foreach中如何獲取數(shù)組下標(biāo)/index
這篇文章主要介紹了javascript foreach中如何獲取數(shù)組下標(biāo)/index問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
JS實(shí)現(xiàn)的簡(jiǎn)單標(biāo)簽點(diǎn)擊切換功能示例
這篇文章主要介紹了JS實(shí)現(xiàn)的簡(jiǎn)單標(biāo)簽點(diǎn)擊切換功能,涉及javascript事件響應(yīng)及頁(yè)面元素遍歷、屬性動(dòng)態(tài)變換等相關(guān)操作技巧,需要的朋友可以參考下2017-09-09
浮動(dòng)的div自適應(yīng)居中顯示的js代碼
這篇文章主要介紹了浮動(dòng)的div自適應(yīng)居中顯示的js代碼,有需要的朋友可以參考一下2013-12-12
JS實(shí)現(xiàn)圖片輪播效果實(shí)例詳解【可自動(dòng)和手動(dòng)】
這篇文章主要介紹了JS實(shí)現(xiàn)圖片輪播效果,結(jié)合完整實(shí)例形式分析了javascript可自動(dòng)和手動(dòng)輪播圖的原理、布局與輪播功能相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-04-04

