Reputation: 151
I have app with couple components. I use Vuex to store data so I could use it in different components. Now I need to render some divs using v-for. I do as follows:
VueX/Modules/shows.js
const state = {
shows: [
{
name: 'Hard Luck',
venue: 'The Venue',
time: '6:00 pm'
},
{
name: 'Hard Luck',
venue: 'The Venue',
time: '6:00 pm'
}
]
}
export default {
state
}
Component where I need to use data:
<template>
<div class="dashboard__details dashboard__details--shows"
v-for="show in shows">
<h3 class="dashboard__name">
{{show.name}} @ {{show.venue}}
</h3>
<span class="dashboard__time">{{show.time}}</span>
<div class="dashboard__btnBlock">
<button">Details</button>
</div>
</div>
</template>
<script>
import { mapState } from 'vuex'
import ReportIssue from './ReportIssue'
import InviteFriend from './InviteFriend'
export default {
name: 'dashboard',
components: { ReportIssue, InviteFriend },
computed: mapState({
shows: state => {
return state.shows
}
})
}
</script>
It works if I have data in the component's data but I can't make it work if I store data in Vuex.
Upvotes: 2
Views: 2993
Reputation: 6574
if you are using a module then you need to of configured that within your store:
# your Vuex store
import shows from './VueX/Modules/shows.js'
export default new Vuex.Store({
modules: {
shows,
},
...
Then when you reference it within a component you call the module not the root state:
export default {
name: 'dashboard',
components: { ReportIssue, InviteFriend },
computed: mapState({
shows: ({ shows }) => shows.shows
})
}
You probably want to rename the module so to avoid shows.shows
but you should get the idea
Upvotes: 6