Search code examples
javascriptvue.jsvuexcomputed-properties

How to call a component function if a data changes in store


I want to trigger a method(serviceSelected) when state of a variable changes. I am trying to use computed but it is not working. My variable is in vuex store which is having a getter as getServiceSelected

computed: {
...mapGetters(["getServiceSelected"]),
serviceChanged() {

  this.serviceSelected(this.getServiceSelected);
  return this.getServiceSelected
}

}


Solution

  • Use watcher instead

    computed: {
        ...mapGetters(["getServiceSelected"])
    },
    watch: {
        getServiceSelected(newValue, oldValue) {
            // do something
            this.serviceSelected(newValue);
        }
    }