Vuex: Accessing store.state.enabled directly from v-if or v-show

Created on 11 Sep 2016  路  2Comments  路  Source: vuejs/vuex

Lets say my vuex store looks like this:

var store = new Vuex.Store({
  state: {
    enabled: true
  },
  mutations: {
    TOGGLE (state) {
      state.enabled = !state.enabled
    }
  }
})

Now when I declare my component in HTML I use:

<my-component v-show="store.state.enabled"></my-component>

I tried this and it didn't work.

Is there any way to access store.state.enabled directly?

I had to use a computed property which seemed more verbose than I was expecting.

computed: {
  StoreStateEnabled () {
    return store.state.enabled
  }
}

Most helpful comment

this.$store is injected to components so you can just use $store in templates.

All 2 comments

this.$store is injected to components so you can just use $store in templates.

I don't think this is true anymore, I can use $store in my template and it works.

Was this page helpful?
0 / 5 - 0 ratings