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,
},
答案 0 :(得分:1)
您是否需要在监视程序中使用options
,不能在mount
或created
上运行它?
此外,watch
函数的确会返回unwatch
,尽管那样您将需要再次设置观察者。看到这里:
https://vuejs.org/v2/api/#vm-watch。
可能更简单的解决方案是在变量中将变量设置为true
。 route
观看并在选项观看中进行检查。
像这样:
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
}
},