当另一个监视程序正在运行时,取消监视任何监视程序

时间:2020-02-24 15:34:16

标签: vue.js vuejs2 vue-component vue-router

watch: {
options: {
      handler () {
        this.get_re_list() 
            .then(data => {
                this.re = data.result
                this.totalM = data.count
            })
      },
      deep: true,
    },
'$route.params.homeCode':  {
  handler () {
    return new Promise((resolve, reject) => {
          reApi.getRe({page:'', count:'', home:this.$route.params.homeCode}) 
            .then(re => {
                this.re = re.result
                this.totalRe = re.count
            })
            .catch(error => {
                return reject(error)
            })
      })
  },
  deep: true,
  immediate: true
}
},

我有路由路径,因为path: '/:homeCode?', homeCode是可选的,因此我在最后保留了?问题::当我通过homeCode时,将调用'$route.params.homeCode',但是会立即执行下一条路由,即optionsoptions运行时如何停止执行'$route.params.homeCode'观察程序?

Edit1:

我可以选择外部分页:

options: {
          page: 1,
          itemsPerPage: 40,
        },

1 个答案:

答案 0 :(得分:1)

您是否需要在监视程序中使用options,不能在mountcreated上运行它? 此外,watch函数的确会返回unwatch,尽管那样您将需要再次设置观察者。看到这里:
https://vuejs.org/v2/api/#vm-watch
可能更简单的解决方案是在变量中将变量设置为trueroute观看并在选项观看中进行检查。
像这样:

watch: {
options: {
      handler () {
        if (this.routeCalled) return
        this.get_re_list() 
            .then(data => {
                this.re = data.result
                this.totalM = data.count
            })
      },
      deep: true,
    },
'$route.params.homeCode':  {
  handler () {
    this.routeCalled = true
    return new Promise((resolve, reject) => {
          reApi.getRe({page:'', count:'', home:this.$route.params.homeCode}) 
            .then(re => {
                this.re = re.result
                this.totalRe = re.count
            })
            .catch(error => {
                return reject(error)
            })
      })
  },
  deep: true,
  immediate: true
}
},