百变鹏仔-专注前端行业精选
当前位置:网站首页 > 最近更新 > 前端开发 > JavaScript > 正文

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

作者:鹏仔先生日期:2020-06-03 14:19:54浏览:2736分类:JavaScript


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

手机扫码访问

暂无评论,来添加一个吧。

取消回复欢迎 发表评论:

关灯