How to use mapState/Mutation/Action in Component?
See original GitHub issueHi,
it would be a huge help to add to the docs how to access state/getters, commit mutations and dispatch actions.
thank you very much for your work and help.
this seems a bit verbose:
<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator'
import { mapState } from 'vuex'
@Component({
computed: {
...mapState('core', ['language'])
}
})
export default class HelloWorld extends Vue {
public language!: string // is assigned via mapState
setLanguage(lang: string) {
this.$store.commit('core/setLanguage', lang)
}
}
</script>
I am sure there is a more elegant way to do this?
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (1 by maintainers)
Top Results From Across the Web
Scroll to mapped Components CORRECTLY in React
In this video I show you how to map refs onto components correctly in react ... In most videos I use Tabnine as...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
In component. we need to declare a getter to get module state, like this:
I think it will be better, if we can do it like this:
I am having issues with MutationAction I was making use of action then I saw there was a better way to do both at the same time.
`import { Module, VuexModule, MutationAction } from “vuex-module-decorators”; import store from “…/index”; import { Api } from “@/api”
@Module({ namespaced: true, name: “properties”, store: store }) export default class Properties extends VuexModule { sellerProperties = {} @MutationAction async getSellerProperties() { await Api().get(“/api/v1/property/fetch-properties”, this.context.rootState.user.user._id).then(response => { this.sellerProperties = response.data.data return {} }).catch(err => { console.log(err.response) console.log(err) }) } } `
This is the code for the particular module. On my browser I have an error: Could not perform action getSellerProperties It also says Cannot read property ‘rootState’ of undefined In my server I have the error: Argument of type ‘Properties’ is not assignable to parameter of type ‘void’. I tried
@MutationAction({mutate: ["sellerProperties"]})
But it still threw the same error. Please could someone explain this to me?