vue 子页面通过暴露属性,实现主页面的某事件的触发

编程入门 行业动态 更新时间:2024-10-24 10:16:13

vue 子页面通过暴露<a href=https://www.elefans.com/category/jswz/34/1771415.html style=属性,实现主页面的某事件的触发"/>

vue 子页面通过暴露属性,实现主页面的某事件的触发

目录

    • 1.前言
    • 2.代码
      • 2-1 子页面
      • 2-2 主页面

1.前言

需求:当我在子页面定义了一个定时器,点击获取验证码,计时器开始倒计时,在这个定时器没有走完,退出关闭子页面,再次进入子页面,定时器此时会被刷新,但是我的需求是,再次进去子页面时,只要上次的计时器没有走完,就继续走,那么,这时候就可以通过暴露属性事件,去触发主页面的事件
具体思路:在主页面定义也定义一个处理的定时器的事件,当在子页面点击获取验证码,开始倒计时,同时调用这个暴露的事件,
那么,这时,主页面就会触发定时器事件,然后将这个倒计时的数据提取出来(当然肯定有其他的方法去处理,但是我不太会前端,只有想到这种方式,还望大佬指点)

2.代码

2-1 子页面

2.1.1 定义属性,将其暴露出来

  props: {withdrawals: {type: Object,default() {return {}},},withdrawalWaitTime:{type: Number,default: 0},handWithdrawalWaitTime:{type: Function,default: null},closeWithdrawals: {type: Function,default: null}}

2.1.2 在钩子函数里定义一个处理每次的计时,每次从主页面过来时,都会携带一个计时参数 withdrawalWaitTime,根据这个值进行一个具体多久的倒计时

async mounted() {await this.fetchData()this.handMobile()this.handWithdrawalWaitTime2()},methods: {handWithdrawalWaitTime2(){this.isWithdrawalsVerify = truethis.coolDownTime = this.withdrawalWaitTimethis.interval = setInterval(() => {this.coolDownTime--;console.log(`coolDownTime:${this.coolDownTime}`)if (this.coolDownTime <= 0) {this.isWithdrawalsVerify = false;clearInterval(this.interval)}}, 1000)},}

2.13 子页面页面按钮

<basic-el-row><basic-el-col><el-form-item label="短信验证码"prop="messageCheckCode"v-if="this.withdrawals.withdrawMsgCheck === 'ENABLE'"><el-inputv-model="withdrawalsParam.messageCheckCode"clearablemaxlength="60"></el-input></el-form-item></basic-el-col><basic-el-col><el-button type="primary" plain:disabled="isWithdrawalsVerify"@click="sendWithdrawCode"v-if="this.withdrawals.withdrawMsgCheck === 'ENABLE'"><span v-show="!isWithdrawalsVerify">获取</span><div v-show="isWithdrawalsVerify && this.coolDownTime > 0"><span class="codeText">{{ this.coolDownTime }}</span><span style="margin-left: 8px">秒</span></div></el-button></basic-el-col>
</basic-el-row>


2.1.4 点击事件,调用暴露的函数事件 (this.handWithdrawalWaitTime()),会触发到对应主页面 事件

        sendWithdrawCode() {this.$refs['withdrawals-form'].validateField('withdrawalsAmount').then((valid) => {if (valid) {this.withdrawSendCodeParam = {memberId: this.withdrawals.id,withdrawalsAmount: this.withdrawalsParam.withdrawalsAmount}this.isWithdrawalsVerify = true;this.coolDownTime = 120;withdrawSendCode(this.withdrawSendCodeParam).then((response) => {this.$message.success("发送短信验证码请求成功,请您注意查收")}).catch((error) => {this.withdrawalsParam = this.tempWithdrawalsParamconsole.error('send withdraw code error', error)})this.handWithdrawalWaitTime()this.interval = setInterval(() => {this.coolDownTime--;if (this.coolDownTime <= 0) {this.isWithdrawalsVerify = false;clearInterval(this.interval)}}, 1000)}});}

2-2 主页面

2.2.1 主页面以组件的形式引入子页面,定义一个抽屉,传入的属性和事件,子页面点击获取验证码时会触发handWithdrawalWaitTime()事件,并将计时的参数携带过去

        <basic-drawerv-model="withdrawalsVisible"title="余额查询信息":footer="false"><withdrawals :closeWithdrawals="closeWithdrawalsVisible":withdrawals="withdrawalsParam":withdrawal-wait-time ="withdrawalWaitTime":hand-withdrawal-wait-time="handWithdrawalWaitTime"></withdrawals></basic-drawer>

主页面事件,会与子页面的计时同步执行

        handWithdrawalWaitTime(){this.withdrawalWaitTime = 120this.withdrawalInterval = setInterval(() => {this.withdrawalWaitTime--;if (this.withdrawalWaitTime <= 0) {this.isEditMobile = false;clearInterval(this.withdrawalInterval)}}, 1000)},

再从主页面过去时,子页面也会接着计时

更多推荐

vue 子页面通过暴露属性,实现主页面的某事件的触发

本文发布于:2023-11-15 16:59:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1603117.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   页面   主页   vue

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!