如何在关闭后以Vue中的模态重置数据值
因此,我在我的模态中接收到称为business
的对象作为道具,如果business.type
是国际的,那么我希望它在10秒后将其重定向到另一个网站。如果是国家,我想留在网站上。但是,假设我先打开国际模式,然后关闭模式,那么它仍然会将我重定向到另一个网站。我希望只有在等待10秒钟,然后将其重定向到另一个网站,如果我关闭模式,那么第二个是重置的,而我不会被重定向。
我的代码现在看起来像这样:
<template>
<modal :show="show" @close="onClose()">
<div v-if="business.type==='international'">
redirecting in {{seconds}}
</div>
<div v-if="business.type==='national'">
Welcome to our page
</div>
</modal>
</template>
<script>
export default {
props: {
show: {
type: Boolean,
required: false,
},
business: {
type: Object,
required: true,
},
},
data() {
return {
seconds: 10,
showingModal: false,
}
},
watch: {
show(val) {
this.showingModal = val
if (this.showingModal) this.countDownTimer()
},
},
computed: {
newBusiness() {
return this.business
},
},
methods: {
countDownTimer() {
if (this.seconds > 0) {
setTimeout(() => {
this.seconds -= 1
this.countDownTimer()
}, 1000)
}
if (this.seconds === 0 && this.newBusiness.type === 'international') {
this.$emit('close')
window.location.replace(`${this.business.website}`)
}
},
onClose() {
this.seconds = 10
this.$emit('close')
},
},
}
</script>
So I receive this object called business
in my modal as a prop and if business.type
is international then I want it to redirect to another website after 10 seconds. If it is national then I want to stay in the website. However, suppose if I open the international modal first and then close the modal then it still redirects me to the another website. I want that only if I wait for the 10 seconds then I get redirected to the other website and if I close the modal then the second is reset and I don't get redirected.
My code looks like this right now:
<template>
<modal :show="show" @close="onClose()">
<div v-if="business.type==='international'">
redirecting in {{seconds}}
</div>
<div v-if="business.type==='national'">
Welcome to our page
</div>
</modal>
</template>
<script>
export default {
props: {
show: {
type: Boolean,
required: false,
},
business: {
type: Object,
required: true,
},
},
data() {
return {
seconds: 10,
showingModal: false,
}
},
watch: {
show(val) {
this.showingModal = val
if (this.showingModal) this.countDownTimer()
},
},
computed: {
newBusiness() {
return this.business
},
},
methods: {
countDownTimer() {
if (this.seconds > 0) {
setTimeout(() => {
this.seconds -= 1
this.countDownTimer()
}, 1000)
}
if (this.seconds === 0 && this.newBusiness.type === 'international') {
this.$emit('close')
window.location.replace(`${this.business.website}`)
}
},
onClose() {
this.seconds = 10
this.$emit('close')
},
},
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
关闭模式时,将超时保存在变量中以清除它(停止倒计时循环)。
在这里,有一个可变“计时器”的景象:
Save your Timeout in a variable to clear it (stop your countDown loop) when you close your modal.
Here an exemple with a variable "timer":
您可以保存从
settimeout
函数返回的计时器ID。关闭模态后,使用
cleartimeout
清除时间出去You can save the timer id, which is returned from
setTimeout
function.and after close the modal, clear time out using
clearTimeout
重定向前无需关闭模态。
There's no need to close the modal before redirection.