本篇文章给大家分享的是有关Vue 中怎么实现一个短信验证码组件,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
创新互联建站专注于惠阳网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供惠阳营销型网站建设,惠阳网站制作、惠阳网页设计、惠阳网站官网定制、微信小程序定制开发服务,打造惠阳网络公司原创品牌,更为您提供惠阳网站排名全网营销落地服务。1、 Vue 组件代码如下:
Vue.component('timerBtn',{ template: '<button v-on:click="run" :disabled="disabled || time > 0">{{ text }}</button>', props: { second: { type: Number, default: 60 }, disabled: { type: Boolean, default: false } }, data:function () { return { time: 0 } }, methods: { run: function () { this.$emit('run'); }, start: function(){ this.time = this.second; this.timer(); }, stop: function(){ this.time = 0; this.disabled = false; }, setDisabled: function(val){ this.disabled = val; }, timer: function () { if (this.time > 0) { this.time--; setTimeout(this.timer, 1000); }else{ this.disabled = false; } } }, computed: { text: function () { return this.time > 0 ? this.time + 's 后重获取' : '获取验证码'; } } });
2、使用方式:
<timer-btn ref="timerbtn" class="btn btn-default" v-on:run="sendCode" :disabled="disabled" :second="60"></timer-btn>
disabled 建议不要绑定,我们可以通过调用组件的setDisabled方法来切换按钮可用状态;
second 初始值60s 没特别值可以不绑定;
所以我们可以在HTML页面这样:
<timer-btn ref="timerbtn" class="btn btn-default" v-on:run="sendCode" ></timer-btn>
JS这样:
var vm = new Vue({ el:'#app', methods:{ sendCode:function(){ vm.$refs.timerbtn.setDisabled(true); //设置按钮不可用 hz.ajaxRequest("sys/sendCode?_"+$.now(),function(data){ if(data.status){ vm.$refs.timerbtn.start(); //启动倒计时 }else{ vm.$refs.timerbtn.stop(); //停止倒计时 } }); }, } });
以上就是Vue 中怎么实现一个短信验证码组件,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联网站建设公司行业资讯频道。
另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
文章名称:Vue中怎么实现一个短信验证码组件-创新互联
文章源于:https://www.cdcxhl.com/article36/dsocsg.html
成都网站建设公司_创新互联,为您提供云服务器、做网站、网站设计、外贸建站、网站改版、微信公众号
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联