如何在关闭后以Vue中的模态重置数据值

发布于 2025-02-01 04:18:47 字数 1469 浏览 0 评论 0原文

因此,我在我的模态中接收到称为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

压抑⊿情绪 2025-02-08 04:18:47

关闭模式时,将超时保存在变量中以清除它(停止倒计时循环)。
在这里,有一个可变“计时器”的景象:

<script>
export default {
 data() {
    return {
      seconds: 10,
      showingModal: false,
      timer: null,
    }
  },
  methods: {
    countDownTimer() {
      if (this.seconds > 0) {
        this.timer = 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() {
      if (this.timer) clearTimeout(this.timer)
      this.seconds = 10
      this.$emit('close')
    },
  },
}
</script>

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":

<script>
export default {
 data() {
    return {
      seconds: 10,
      showingModal: false,
      timer: null,
    }
  },
  methods: {
    countDownTimer() {
      if (this.seconds > 0) {
        this.timer = 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() {
      if (this.timer) clearTimeout(this.timer)
      this.seconds = 10
      this.$emit('close')
    },
  },
}
</script>
我爱人 2025-02-08 04:18:47

您可以保存从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

倾城花音 2025-02-08 04:18:47

重定向前无需关闭模态。

<template>
 <modal :show="show" @close="$emit('close')">
   <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,
    }
  },
  
  mounted(){
    if(this.business.type === 'international'){
      setInterval(() => {
        if(this.seconds < 1){
          window.location.replace(`${this.business.website}`)
        }

        --this.seconds;
      }, 1000);
    }
  }
}
</script>

There's no need to close the modal before redirection.

<template>
 <modal :show="show" @close="$emit('close')">
   <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,
    }
  },
  
  mounted(){
    if(this.business.type === 'international'){
      setInterval(() => {
        if(this.seconds < 1){
          window.location.replace(`${this.business.website}`)
        }

        --this.seconds;
      }, 1000);
    }
  }
}
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文