Vue2 监听 Store Getter 变化并发送请求
在 Vue 组件的 'created' 生命周期钩子中,可以监听 'this.$store.getters.userid' 的变化,并在变化后赋值给 'this.userId',然后发送请求。
created() {
this.userId = this.$store.getters.userid;
this.fetchData(); // 发送请求
},
watch: {
'$store.getters.userid': function(newUserId) {
this.userId = newUserId;
this.fetchData(); // 发送请求
}
},
methods: {
fetchData() {
// 发送请求的逻辑
}
}
在 'created' 生命周期钩子中,将 'this.$store.getters.userid' 的值赋给 'this.userId'。然后,在 'watch' 属性中监听 'this.$store.getters.userid' 的变化,一旦变化,就将新的值赋给 'this.userId',然后调用 'fetchData' 方法发送请求。
原文地址: https://www.cveoy.top/t/topic/p2S4 著作权归作者所有。请勿转载和采集!