I have a component that contains some checkboxes. I need to be able to access which checkboxes are checked from other components in my Vue application, but I cannot for the life of me figure out (nor find online) how to properly connect the checkboxes to my Vuex store.
What is the right way to connect checkboxes within a component to a Vuex store, so that they act just as if the checkbox was connected to the components data via v-model?
Here is a starting point for what I'm trying to do (in a very very basic sense)
Code in jsfiddle below
https://jsfiddle.net/9fpuctnL/
The aim is to get the colours that are selected in the colour-checkboxes component to output in the chosen-colours component, going through the Vuex store.
I wanted to provide an answer that actually uses checkboxes.
There is one possible solution outlined here: Vuex dynamic checkboxes binding
And a simpler solution can be achieved something like the following:
Key here is calling a method using on-change, it will pass an event to the method with all the details needed to make the change.
The @change function:
Here I want the value, in this case will be the tenants name, but further inspection of the target also gives the 'id', and whether or not the checkbox is 'checked' or unchecked.
Over in the store, we can manipulate the 'selectedTenants' array:
Here are the actual mutators:
The vuejs docs are great, but sometimes they can be a little light on with real world examples. I don't think it's possible to achieve the above using a computed value, with get(), set()... but I'd like to see a solution that can.
OK I have been challenged to show my solution. Here it is on jsfiddle
the html is:
and the js is:
You shoud remove colours = [] in data.
You can use computed property with getter as vuex getter and setter in computed property which will call a mutation for that state property to do this.
You can see an example of this here with two-way Computed Property:
Use
@change
to update Vuex as needed:HTML:
JS: