首页 > 解决方案 > 更改网址后立即停止观看

问题描述

我想知道如何改变这个

computed: {
    currentUrl () {
      return this.$route.path
    },
  },
  watch: {
    currentUrl (newURl, firstUrl) {
      if ((!this.$cookiz.get('firstUrl')) && ((this.check === undefined) || (this.check === 0))) {
        this.showPopUp()
      }
    }
  },

手表在检测到第一次更改或网址后停止一次。任何想法将不胜感激。非常感谢。

标签: vue.js

解决方案


与其在内部创建观察者watch,不如在钩子内部创建它created,然后通过调用自身来删除它:

created() {
  this.unwatchCurrentUrl = this.$watch('currentUrl', (newURl, firstUrl) => {
    const condition1 = !this.$cookiz.get('firstUrl')
    const condition2 = this.check === undefined
    const condition3 = this.check === 0
    if (condition1 && (condition2 || condition3)) {
      this.showPopUp()

      // Remove watcher
      this.unwatchCurrentUrl()
    }
  })
}

推荐阅读