When I call a mapped Vuex action in my mounted hook, the action works, but I get "TypeError: xxx is not a function" error in the console.
Here's my whole script section for this component:
<script>
import SideNav from '@/components/SideNav.vue'
import ActionBar from '@/components/ActionBar.vue'
import Summaries from '@/components/Summaries.vue'
import { mapState, mapActions } from 'vuex'
export default {
components: { SideNav, ActionBar, Summaries },
computed: {
...mapState(['dataLoading']),
...mapActions(['init'])
},
mounted() {
this.init();
}
}
</script>
Actions are similar to mutations, the differences being that: Instead of mutating the state, actions commit mutations. Actions can contain arbitrary asynchronous operations.
Mapping in Vuex enables you to bind any of the state's properties, like getters, mutations, actions, or state, to a computed property in a component and use data directly from the state. Although we can get the job done with this. $store.state.user.data.name , we can use a map helper to simplify it to this.
An action in Vuex is where you perform interaction with APIs and commit mutations. Such interactions are inherently asynchronous.
You should map Actions as methods
instead of computed
, see dispatch actions in components:
computed: {
...mapState(['dataLoading'])
},
methods: {
...mapActions(['init'])
},
mounted() {
this.init();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With