Skip to content

Instantly share code, notes, and snippets.

@alizhdanov
Created November 16, 2017 15:29
Show Gist options
  • Save alizhdanov/9ceae830ab74ad2e1346de746824e8f1 to your computer and use it in GitHub Desktop.
Save alizhdanov/9ceae830ab74ad2e1346de746824e8f1 to your computer and use it in GitHub Desktop.
Vuex cheat sheet

STATE

  • Inside store
state: {
  count: 0
}
  • Inside component
computed: {
  count () {
    return this.$store.state.count
  }
}
  • The mapState Helper
// in full builds helpers are exposed as Vuex.mapState
import { mapState } from 'vuex'

export default {
  // ...
  computed: 
    ...mapState({
      count: state => state.count,

      countAlias: 'count',

      // to access local state with `this`, a normal function must be used
      countPlusLocalState (state) {
        return state.count + this.localCount
      }
  })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment