[英]Vue.js - Is It bad practice to use a local method within computed?
我正在使用 Vuex state 作为事件(eventBus)的触发器。
我通过以下方式触发事件:
this.$store.dispatch('triggerRefetch');
并使用以下方法处理它:
computed: {
trigger: function(){
return this.$store.getters['triggerFlag']
}
},
watch: function(newV, oldV){
this.handleTrigger();
},
在这里,我不需要新值也不需要旧值。 所以相反,可以只使用计算这样的:
computed: {
trigger: function(){
this.handleTrigger();
return this.$store.getters['triggerFlag']
}
},
以这种方式对 Vuex(事件总线)使用计算是否是一种不好的方法? 另外还有更好的方法吗?
我认为在计算属性中运行一些额外的逻辑是一种不好的做法,属性只是一个返回一些数据的属性,它不是 function 或运行一堆逻辑的方法,请保留第一种方法,这是最好的可读性和可扩展性,使您的代码遵循一些清晰的流程。
computed: {
trigger: function(){
return this.$store.getters['triggerFlag']
}
},
watch: {
trigger: function(){
this.handleTrigger();
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.