2
votes

enter image description here

I'm trying to pass a list of button names into a menu component from the Vuex store following https://nuxtjs.org/guide/vuex-store

my /store/store.js:

export const state = () => ({
    'toolbarActions' : [ 'My project', 'Home', 'About', 'Contact' ]
})

My menu component:

<template>
  <v-toolbar color="indigo" dark>
    <v-toolbar-side-icon></v-toolbar-side-icon>
    <v-toolbar-title class="white--text">Title</v-toolbar-title>
    <v-spacer></v-spacer>
    <v-toolbar-items class="hidden-sm-and-down">
       <v-btn flat v-for="action in toolbarActions" :key="action">{{action}}</v-btn>
             <!-- <v-btn flat v-for="action in toolbarActions">{{action}}</v-btn> -->
      <!-- <v-btn flat>Link One</v-btn>
      <v-btn flat>Link Two</v-btn>
      <v-btn flat>Link Three</v-btn> -->
    </v-toolbar-items>
  </v-toolbar>
</template>

<script>

// import toolbarActions from '~/store/store.js'

export default {
computed: {
  toolbarActions() {
          return this.$store.state.toolbarActions

          // return [ 'My project', 'Home', 'About', 'Contact' ]
  }
  }
}
</script>

If I uncomment:

      // return [ 'My project', 'Home', 'About', 'Contact' ]

and comment:

          return this.$store.state.toolbarActions

The button names are passed into the component. but with

 return this.$store.state.toolbarActions

not commented, nothing is passed in.

How do I access the Vuex store here to pass in the button names?

EDIT: I've made the changes, I'm getting:

   ERROR  [Vue warn]: Error in render: "TypeError: Cannot read property 
 'toolbarActions' of undefined"                                                                                                           
  11:52:20

  found in

 ---> <Menu> at components/menu.vue
   <Default> at layouts/default.vue
     <Root>

 ยป store\_toolbar.js   
2
what's the issue ? - Boussadjra Brahim
@BoussadjraBrahim is that clearer? - user1592380
yes it's clear but i recommend to share the store code as text not as screenshot - Boussadjra Brahim
Thanks very much! - user1592380
from those docs - > We don't need to install vuex since it's shipped with Nuxt.js. We can now use this.$store inside our components: <template> <button @click="$store.commit('increment')">{{ $store.state.counter }}</button> </template> - user1592380

2 Answers

3
votes

I recommend to use a module named toolbar inside it put the following code :

  export const state = () => ({
     'toolbarActions' : [ 'My project', 'Home', 'About', 'Contact' ]
   })

the folder structure should be like :

.
.
> static
v store
  |_toolbar.js

and your computed property should be like :

computed: {
  toolbarActions() {
      return this.$store.state.toolbar.toolbarActions  //look i added the name of the toolbar module
                              // ^___________

  }
 }
}
-2
votes

Better option may be

import {mapGetters} from 'vuex';

and use like

computed:mapGetters({
    toolbarActions:'toolbar/toolbarActions'
})