Search code examples
javascriptvue.jsvuejs2vue-componentvuex

Vue.js - Is It bad practice to use a local method within computed?


I am using Vuex state as a trigger of an event (eventBus).

I trigger the event with:

this.$store.dispatch('triggerRefetch');

and process it with:

computed: {
    trigger: function(){ 
        return this.$store.getters['triggerFlag'] 
    }
},
watch: function(newV, oldV){
    this.handleTrigger();
},

Here I don't need the new value nor the old value. So instead, is it okay to just use computed for this like:

computed: {
    trigger: function(){ 
        this.handleTrigger();
        return this.$store.getters['triggerFlag'] 
    }
},

Would it be a bad way to use computed this way for Vuex (event bus)? In addition is there even a better way?


Solution

  • I see that's a bad practice to run some extra logic inside the computed property, a property is a just a property that returns some data it's not a function or a method that run a bunch of logic, keep the first approach which is best in terms of readability and extensibility, that allow your code to follow some clear flow.

    computed: {
        trigger: function(){ 
            return this.$store.getters['triggerFlag'] 
        }
    },
    watch: {
     trigger: function(){
        this.handleTrigger();
    },