In Vuex, use dispatch to trigger mutations to change state data. Use dispatch to store a value: this.$store.dispatch('setValue', 10); use getters to derive data from state: getters: { getValue: (state) => { return state.value; } }Use mapGetters in the component to access the getter: computed: { ...mapGetters({ value: 'getValu
Use dispatch in Vue to store the value and get
in In Vuex state management, the dispatch
method is used to trigger mutations. These mutations can change the data stored in the Vuex state. To use
to store a value, you can. Pass a value as a parameter to a mutation. For example: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><code class="javascript">this.$store.dispatch('setValue', 10);</code></pre><div class="contentsignin">Copy after login</div></div>
In this example,
is a specified mutation that stores the value 10 in the Vuex state. ##To get the stored value, you can use getters
. Getters are computed properties derived from Vuex state, which allow you to access and manipulate state data.
To create getters, you can use Vuex. Using the getters
option in the module:
<code class="javascript">getters: { getValue: (state) => { return state.value; } }</code>
Then, you can use the mapGetters
helper function in the component to access the getter:
<code class="javascript">computed: { ...mapGetters({ value: 'getValue', }), }</code>
Now, you can Access the stored value using this.value
Full example:
<code class="javascript">// Vuex 模块 const module = { state: { value: null, }, mutations: { setValue(state, value) { state.value = value; }, }, getters: { getValue: (state) => { return state.value; } } }; // Vue 组件 export default { computed: { ...mapGetters({ value: 'getValue', }), }, methods: { setValue() { this.$store.dispatch('setValue', 10); }, }, };</code>
The above is the detailed content of How to get the dispatch value in vue. For more information, please follow other related articles on the PHP Chinese website!