1

I am new to both vue.js and vuex. I have a component that need to dispatch an action when a specific data is available in the state. How can I do this.

Example:

export default {
   name: 'MyComponent',
   computed: {
      targetItem() {
         return this.$store.getters.getTarget(this.$route.params.id);
      }
   }
}

In the example above i would like to dispatch a new action on the store when targetItem has a value. This is so i can trigger an ajax request via a vuex action to collect more data about targetItem

Thanks

2
  • What have you already tried and what's not working as you expected? Commented Aug 11, 2017 at 20:02
  • Your use-case is very unclear to me. Are you looking for something like in this fiddle? Please add more details to your questions. What are you planning to do with the action? Is it sending an ajax request, does it change state etc.? Commented Aug 11, 2017 at 21:38

1 Answer 1

1

I eventually found a solution that work

export default {
   name: 'MyComponent',
   computed: {
      targetItem() {
         return this.$store.getters.getTarget(this.$route.params.id);
     }
   },
   watch: {
      shop (newVal, oldVal) {
         if(!!newVal && !oldVal) {
            const {targetItem} = this;           
            this.$store.dispatch('ACTION_NAME',{targetItem});
         }
      }
   }
}

Thanks

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.