I'm trying to use Vuex and I need to use methods inside a computed property as it is in my real project. I can't figure it out how to call the method and store the data in Vuex.
This is a simple example to show what I need to achieve:
my store:
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
export const store = new Vuex.Store({
state: {
calculations: {
number: 1,
letter: 'a',
combinationA:'test',
}
},
getters:{
},
mutations:{
commit_number(state, val){state.calculations.number = val},
commit_letter(state, val){state.calculations.letter = val},
commit_combinationA(state, val){state.calculations.combinationA = val},
},
actions: {
}
})
and this is my component
<template>
<div>
<div>
choose a number:
<input v-model="number" type="number">
</div>
<br>
<br>
<select v-model="letter" >
choose a letter
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
</select>
<p>combination A: {{ combinationA }}</p>
<p>combination A without Vuex: {{ combination_no_vuex }}</p>
</div>
</template>
<script>
export default {
methods: {
MethodCombinationA(letter, number){
let result = String(letter).concat(number);
return result;
},
},
computed: {
number: {
get () { return this.$store.state.calculations.number;},
set (value) { this.$store.commit('commit_number', value); }
},
letter: {
get () { return this.$store.state.calculations.letter;},
set (value) { this.$store.commit('commit_letter', value ); }
},
combinationA: {
get () { return this.$store.state.calculations.combinationA;},
// set (value) { this.$store.commit('commit_combinationA', this.MethodCombinationA(this.letter, this.number) ); }
//
// not working, this is the idea
},
combination_no_vuex() {
return this.MethodCombinationA(this.letter, this.number);
}
}
}
</script>
Of course the computed property 'combination_no_vuex' is working as expected, and I would like to store/commit 'combinationA' as 'combination_no_vuex' and then using it in other components. I know Is pretty basic thing, but I'm stuck at the moment!
--edit-- I'm pretty sure this is not the best idea, but somehow it works:
I added an action:
actions: {
commit_combinationA(context, payload) {
context.commit('commit_combinationA', payload);
}
}
then, when I get the computed property, it fires the method, then I dispatch it using a watch.
<template>
<div>
<div>
choose a number:
<input v-model="number" type="number">
</div>
<br>
<br>
<select v-model="letter" >
choose a letter
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
</select>
<p>combination A: {{ combinationA }}</p>
<p>combination A alias: {{ combinationAalias }}</p>
<p>combination A without Vuex: {{ combination_no_vuex }}</p>
</div>
</template>
<script>
import { mapGetters, mapActions } from 'vuex';
export default {
methods: {
MethodCombinationA(letter, number){
const result = String(letter).concat(number);
return result;
},
},
computed: {
number: {
get () { return this.$store.state.calculations.number;},
set (value) { this.$store.commit('commit_number', value); }
},
letter: {
get () { return this.$store.state.calculations.letter;},
set (value) { this.$store.commit('commit_letter', value ); }
},
combinationA: {
get () { return this.MethodCombinationA(this.letter, this.number)},
},
combinationAalias(){
return this.$store.state.calculations.combinationA;
},
combination_no_vuex() {
return this.MethodCombinationA(this.letter, this.number);
}
},
watch: {
combinationA(value){
this.$store.dispatch('commit_combinationA', value );
}
},
}
</script>