vue中使用watch监听Vuex中存储的值变化

内容时效提醒:本文最后更新于 2020年8月22日,部分内容可能已经过时,请结合实际情况参考。

需求是当vuex中存储的PageId 发生变化,则让定义的index的值等于 PageId,那么就得实时监听PageId的变化

computed 中

computed: {
	monitor () {
		return this.$store.state.PageId
	}
}

watch 中

watch: {
	monitor () {
		this.index = this.$store.state.PageId
	}
}

computed 和 watch 中必须定义的函数名一样,如上代码 computed 中为 monitor,那么 watch 中也需一样为 monitor

发表回复

电子邮件地址不会被公开,必填项已用*标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理