微信小程序自定义组件示例
声明:此文借鉴两位前辈的博文
https://www.jianshu.com/p/8a2a730d9e60
http://www.jb51.net/article/117295.htm
感谢这两位前辈的贡献!!!
实现功能:把密码输入封装成一个自定义组件,可以多处调用此组件
component部分:
pwdalert.wxml
<view wx:if="{ { pwd_flag}}" class="password"> <view class="input-content-wrap"> <view class="top"> <view catchtap="close_pwd_alert" class="close">×</view> <view class="txt">{ { pwdtitle}}</view> <view catchtap="modify_password" class="forget"></view> </view> <view class="actual_fee"> <span>{ { currency}}</span> <text>{ { amt}}</text> </view> <view catchtap="setFocus" class="input-password-wrap"> <view class="password_dot"> <i wx:if="{ { pay_password.length>=1}}"></i> </view> <view class="password_dot"> <i wx:if="{ { pay_password.length>=2}}"></i> </view> <view class="password_dot"> <i wx:if="{ { pay_password.length>=3}}"></i> </view> <view class="password_dot"> <i wx:if="{ { pay_password.length>=4}}"></i> </view> <view class="password_dot"> <i wx:if="{ { pay_password.length>=5}}"></i> </view> <view class="password_dot"> <i wx:if="{ { pay_password.length>=6}}"></i> </view> </view> </view> <input bindinput="_pwdEvent" class="input-content" password type="number" focus="{ { isFocus}}" maxlength="6" /> </view>
pwdalert.wxss
page { height: 100%; width: 100%; background: #e8e8e8; }
page .password { position: absolute; left: 0; top: 0; width: 100%; height: 100%; background: rgba(0, 0, 0, 0.7); }
page .password .input-content-wrap { position: absolute; top:300rpx; left: 50%; display: flex; flex-direction: column; width: 600rpx; margin-left: -300rpx; background: #fff; border-radius: 20rpx; }
page .password .input-content-wrap .top { display: flex; align-items: center; height: 90rpx; border-bottom: 2rpx solid #ddd; justify-content: space-around; }
page .password .input-content-wrap .top .close { font-size: 44rpx; color: #999; font-weight: 100; }
page .password .input-content-wrap .top .forget { color: #00a2ff; font-size: 22rpx; }
page .password .input-content-wrap .actual_fee { display: flex; align-items: center; justify-content: center; color: #000; height: 100rpx; margin: 0 23rpx; border-bottom: 2rpx solid #ddd; }
page .password .input-content-wrap .actual_fee span { font-size: 60rpx; }
page .password .input-content-wrap .actual_fee text { font-size: 60rpx; }
page .password .input-content-wrap .input-password-wrap { display: flex; align-items: center; justify-content: center; height: 150rpx; }
page .password .input-content-wrap .input-password-wrap .password_dot { display: flex; align-items: center; justify-content: center; text-align: center; color: #000; box-sizing: border-box; width: 90rpx; height: 90rpx; border: 2rpx solid #ddd; border-left: none 0; }
page .password .input-content-wrap .input-password-wrap .password_dot:nth-child(1) { border-left: 2rpx solid #ddd; }
page .password .input-content-wrap .input-password-wrap .password_dot i { background: #000; border-radius: 50%; width: 20rpx; height: 20rpx; }
page .password .input-content { position: absolute; opacity: 0; left: -100%; top: 600rpx; background: #f56; z-index: -999; }
page .password .input-content.active { z-index: -99; }
pwdalert.json
{
"component": true }
pwdalert.js
Component({
properties: {
pwdtitle: { // 属性名
type: String, // 类型(必填),目前接受的类型包括:String, Number, Boolean, Object, Array, null(表示任意类型)
value: '请输入支付密码', // 属性初始值(可选),如果未指定则会根据类型选择一个
observer: function (newVal, oldVal) { } // 属性被改变时执行的函数(可选),也可以写成在methods段中定义的方法名字符串, 如:'_propertyChange'
},
currency:{ //币种
type:String,
value:'',
},
amt: { //金额
type: String,
value: '',
},
pwdfull: { //
type: String,
value: '',
},
},//组件的对外属性
data: {
isFocus: false,//控制input 聚焦
pwd_flag: false//密码输入遮罩
},//私有数据,可用于模版渲染
methods: {
//组件的方法,包括事件响应函数和任意的自定义方法
close_pwd_alert: function () { //关闭钱包输入密码遮罩
console.log('close pwd alert')
this.setData({
isFocus: false,//失去焦点
pwd_flag: false,
})
},
show_pwd_alert: function () { //关闭钱包输入密码遮罩
console.log('show pwd alert')
this.setData({
isFocus: true,//获得焦点
pwd_flag: true,//获得焦点
})
},
setFocus: function () {
console.log('isFocus', this.data.isFocus)
this.setData({
isFocus: true
})
},
_pwdEvent: function (e) {
var pwd = e.detail.value
// var app = this
// console.log("pwdEvent=="+JSON.stringify(e))
this.setData({
pay_password: pwd
});
if (pwd.length == 6) {
//密码长度6位时
//监听密码输入6位时,要做的事情
console.log("密码长度是6位")
this.data.pwdfull = pwd
}
//自定义组件触发事件时,需要使用 triggerEvent 方法,指定事件名、detail对象和事件选项
// this.triggerEvent("pwdEvent")
this.triggerEvent("pwdEvent",e.detail)
},
},
// 内部方法建议以下划线开头
_propertyChange: function (newVal, oldVal) {
}
})
调用component:
index.wxml
<view class="page"> <view style='padding-left:20rpx;padding-right:20rpx;padding-top:100rpx;'> <button class="weui-btn" type="primary" bindtap="showDialog" style='clear:both'>showDialog</button> </view> <!--调用自定义组件--> <pwddialog id='pwddialog' pwdtitle="{ { pwdtitle}}" currency="{ { currency}}" amt="{ { amt}}" bind:pwdEvent="_pwdEvent" bind:close_pwd_alert="close_pwd_alert" bind:setFocus="setFocus"></pwddialog> </view>
index.wxss
@import "../../utils/weui.wxss";
.primaryfont{ font-size:1.0rem; color:black; }
index.json
{
"usingComponents": { "dialog": "../components/dialog/dialog", "pwddialog":"../components/pwddialog/pwddialog" } }
index.js
Page({
data: {
pwdtitle:"我是标题",
pwdfull:'',
currency:'¥',
amt:'100',
},
onReady: function () { /**生命周期函数--监听页面初次渲染完成 *///获得dialog组件
this.dialog = this.selectComponent("#pwddialog");
},
showDialog: function () { //button点击事件
console.log("click dialog button1")
//显示alert
this.dialog.show_pwd_alert()
console.log("click dialog button2")
},
_pwdEvent(e) { //回调事件
console.log("e.detail==" + JSON.stringify(e.detail))
console.log("e.detail.value==" + JSON.stringify(e.detail.value))
console.log("pwdfull==" + this.dialog.data.pwdfull)
},
});
还没有评论,来说两句吧...