这个问题可能是因为在跳转路由的同时,定时器还在继续执行,导致页面出现错误。解决方法可以是在路由跳转前清除定时器。

可以在beforeRouteLeave中清除定时器,例如:

export default {
  beforeRouteLeave(to, from, next) {
    clearTimeout(this.timer)
    next()
  },
  data() {
    return {
      timer: null
    }
  },
  mounted() {
    this.timer = setTimeout(() => {
      // do something
    }, 1000)
  }
}

这样在路由跳转前会清除定时器,避免出现问题。

nuxt路由跳转不会清除setTimeout定时器

原文地址: http://www.cveoy.top/t/topic/0pM 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录