W
W
WebDev2018-12-19 16:48:54
Vue.js
WebDev, 2018-12-19 16:48:54

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

Then I created a simple component
<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>

Both claims turned out to be false. I can change values ​​directly (changeManually method) and I can output values ​​directly instead of through computed.
My question is: is it just customary to change and apply indirectly, or can it really not work under any circumstances?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Sedyshev, 2018-12-19
@ musikant777

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 question

Ask a Question

731 491 924 answers to any question