Answer the question
In order to leave comments, you need to log in
Is it mandatory to use computed?
I am using Vuex.
The documentation says the following:
You cannot directly change the state of a vault. The only way to make changes is to explicitly invoke the mutation.
Since the Vuex store is reactive, the easiest way to "get" is to simply return part of the store's state in a computed property
<div>
<div>
DATA FROM STORE: {{ $store.state.test }}
</div>
<div>
DATA FROM COMPUTED: {{ storeTest }}
</div>
<hr>
<button @click="changeWithMutation">Randomize test</button>
<button @click="changeManually">Randomize manually</button>
</div>
<script>
export default {
data() {
return {
}
},
computed: {
storeTest() {
return this.$store.state.test;
}
},
created() {
},
methods: {
changeWithMutation() {
this.$store.commit('setTest', Math.random());
},
changeManually() {
this.$store.state.test = Math.random();
}
}
}
</script>
Answer the question
In order to leave comments, you need to log in
Change the state without mutations:
But what if the internal implementation of vuex changes in future versions and the undocumented way stops working?
My opinion: the pros do not outweigh the cons.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question