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

需求是当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

发表回复

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.