uniapp watch用法例子

<template>
    <view class="main-container">
        <view class="list">
            <view class="item" :id="index">
                <view class="text">
                    输入密码
                </view>
                <view class="arrow">
                    <input value="" type="password"  maxlength="20" v-model="password1" placeholder="请输入新密码(6-20位数字)" class="input-class" placeholder-class="pcl"/>
                </view>
            </view>
            <view class="item" :id="index">
                <view class="text">
                    确认密码
                </view>
                <view class="arrow">
                    <input value="" type="password"  maxlength="20" v-model="password2" placeholder="请再次输入密码" class="input-class" placeholder-class="pcl"/>
                </view>
            </view>
        </view>

        <view class="save-pwd" :class="!disabled ? 'active': ''" @tap="clickSave" :disabled="disabled">
            保存
        </view>
    </view>
</template>

<script>
    const app = getApp()
    export default {
        data() {
            return {
                password1: '',
                password2: '',
                disabled:true,
            }
        },
        //监听按钮的颜色
        watch:{
            password1(val){
                this.OnBtnChange();
            },
            password2(val){
                this.OnBtnChange();
            },
        },

        methods:{
            // 改变按钮状态
            OnBtnChange(){
                if( this.password1 && this.password2 ){
                    this.disabled=false;
                    return;
                }
                this.disabled=true;
            },

            clickSave() {
                if(this.password1.trim().length < 6) {
                    uni.showToast({
                        title:'密码不足6位',
                        icon:'none'
                    })
                } else if(this.password1.trim().length > 20) {
                    uni.showToast({
                        title:'密码超过20位',
                        icon:'none'
                    })
                }
                if(!this.password1 || !this.password2 || this.password1.trim() !== this.password2.trim()) {
                    uni.showToast({
                        title:'两次密码不一致',
                        icon:'none'
                    })
                    return false;
                }

                this.$http.post('updatePwd', {
                    userId: app.globalData.userId,
                    password: this.password1
                },{token: 'token'}).then(([error, res])=>{
                    if(res.data.code == 1) {
                        uni.showToast({
                            title:'密码更新成功',
                            icon:'none',
                        })

                        setTimeout(() => {
                            uni.navigateBack({
                                delta:1
                            })
                        },1000)
                    }

                })
            }
        }
    }
</script>

<style>
    .main-container {
        width: 100vw;
        height: 100vh;
        box-sizing: border-box;
        background: #FFFFFF;
        opacity: 1;
        padding-top: 30rpx;
        border-top: 30rpx solid #FBFAF9;
    }
    .list {
        width: 100%;
        display: flex;
        flex-direction: column;
        justify-content: flex-start;
        background-color: #FFFFFF;
    }
    .item {
        width: 670rpx;
        height: 140rpx;
        margin: 0 auto;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    .item:last-child {
        border: 0;
    }
    .item .text {
        height: 140rpx;
        line-height: 140rpx;
        font-size: 32rpx;
        font-family: PingFang SC;
        font-weight: 700;
        color: #000000;
        opacity: 0.8;

    }
    .item .arrow {
        height: 100%;
        flex: 1 0 516rpx;
        border-bottom: 2rpx solid rgba(0,0,0,0.1);
        display: flex;
        justify-content: flex-start;
        align-items: center;
    }
    .pcl {
        font-size: 32rpx;
        font-family: PingFang SC;
        font-weight: 500;
        color: #000000;
        opacity: 0.4;
        text-indent: 20rpx;
    }
    .input-class {
        width: 450rpx;
        padding-left: 15rpx;
        font-size: 32rpx;
    }
    .save-pwd {
        width: 304rpx;
        height: 88rpx;
        background: #FFFFFF;
        border: 2rpx solid #989898;
        opacity: 1;
        border-radius: 46px;
        font-size: 32rpx;
        font-family: PingFang SC;
        font-weight: bold;
        color: #999999;
        opacity: 1;
        margin: 100rpx auto 0;
        display: flex;
        justify-content: center;
        align-items: center;
    }
    .active{
        background: #4DB046;
        color: #ffffff;
        border: 0;
    }
</style>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值