question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use mapState/Mutation/Action in Component?

See original GitHub issue

Hi,

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:closed
  • Created 4 years ago
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
Gcaufycommented, Mar 23, 2020

In component. we need to declare a getter to get module state, like this:

@Component({})
export default class extends Vue {
  get someState() {
     return someModule.state;
  }
}

I think it will be better, if we can do it like this:

@Component({})
export default class extends Vue {
  @MapAction('someModule.state') someState;
}
0reactions
god-of-jscommented, Oct 14, 2020

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found