Vuex。如何使用v-for从商店呈现数据

Evg*_*kiy 2 vue.js vue-component

我有几个组件的应用程序。我使用Vuex来存储数据,因此可以在不同的组件中使用它。现在,我需要使用v-for渲染一些div。我做如下:

VueX /模块/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
}
Run Code Online (Sandbox Code Playgroud)

我需要使用数据的组件:

  <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>
Run Code Online (Sandbox Code Playgroud)

如果组件中的数据中有数据,它将起作用,但是如果我将数据存储在Vuex中,则无法使它起作用。

Guy*_*uyC 5

如果您使用的是模块,则需要在商店中进行配置:

# your Vuex store
import shows from './VueX/Modules/shows.js'
export default new Vuex.Store({
  modules: {
    shows,
  },
  ...
Run Code Online (Sandbox Code Playgroud)

然后,当您在组件中引用它时,您将调用模块而不是root状态:

export default {
  name: 'dashboard',
  components: { ReportIssue, InviteFriend },
  computed: mapState({
    shows: ({ shows }) => shows.shows
  })
}
Run Code Online (Sandbox Code Playgroud)

您可能想重命名模块,以避免,shows.shows但您应该明白