не наблюдать за любым наблюдателем, когда работает другой наблюдатель - PullRequest
0 голосов
/ 24 февраля 2020
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', но сразу же выполняется следующий маршрут, т.е. options. Как мне прекратить выполнение options наблюдателей, когда '$route.params.homeCode' работает?

Edit1:

У меня есть варианты для внешнего разбиения на страницы:

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

1 Ответ

1 голос
/ 24 февраля 2020

Вам нужен options в наблюдателе, не может ли он работать на mount или created? Кроме того, функция watch возвращает unwatch, хотя тогда вам потребуется заново настроить наблюдатель. смотрите здесь:
https://vuejs.org/v2/api/#vm -watch .
Возможно, более простым решением было бы просто установить переменную на true внутри вашего. route смотрите и проверяйте это в опциях watch.
Вот так:

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
}
},
...