发送短信验证码组件

用vue+vant实现一个发送短信验证码组件

<template>
    <van-button type="primary" :disabled="disabled" @click.stop="startCountDown">{{message || initTip}}</van-button>
</template>
<script>
    export default {
        data () {
            return {
                count_time:this.time,
                disabled:false,
                message:'',
                interval:null
            }
        },
        props:{
            /** 需要倒数的时间 */
            time:{
                type:Number,
                default:60
            },
            /** 初始化提示 */
            initTip:{
                type:String,
                default:'发送验证码'
            },
            /** 倒计时结束时的提示*/
            endTip:{
                type:String,
                default:'重新获取'
            },
            /** 倒计时开始后 秒数的后缀*/
            sufTip:{
                type:String,
                default:'秒后重新获取'
            },
            /** 一个异步方法 回调后开始倒计时*/
            start:{
                type:Function,
                required:true
            }
        },
        methods:{
            handelClick() {
                this.start.then(() => this.startCountDown()).catch(()=>{})
            },
            startCountDown() {
                this.disabled = true
                this.interval = setInterval(() => {
                    if(this.count_time > 1) {
                        this.count_time --
                        this.message = this.count_time + this.sufTip
                    }else {
                        clearInterval(this.interval)
                        this.count_time = this.time
                        this.message = '重新发送'
                        this.disabled = false
                    }
                },1000)
            }
        }
    }
</script>
<style>

</style>

父组件使用:

<template>
  <div class="home">
      <van-cell-group>
          <van-field label="图片验证码" placeholder="请输入图片验证码" v-model="code"  center clearable>
              <count-down-btn slot="button" :start="sendCode"></count-down-btn>
          </van-field>
      </van-cell-group>
  </div>
</template>
<script>
import countDownBtn from '@/components/countDownBtn.vue'
export default {
  name: 'home',
  components: {countDownBtn},
    data () {
      return {
          code:''
      }
    },
    mounted () {},
    methods:{
        sendCode () {
          return  new Promise((resolve,reject) => {
              if(!this.code) {
                alert('请输入手机号')
                reject()
              }else {
                  //发送短信验证码接口
                resolve()
              }
          })
        }
    }
}
</script>
<style>
    *{
        margin: 0;
        padding: 0;
    }
    .home{
        width: 100%;
    }
</style>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值