Vue:如何使用商店与组件?

Dre*_*pie 8 vue.js vue-router vue-component vuex vuejs2

//商店

export default {
  state: {
    aboutModels: []
  },
  actions: {
    findBy: ({commit}, about)=> {
      //do getModels
      var aboutModels = [{name: 'About'}] //Vue.resource('/abouts').get(about)
      commit('setModels', aboutModels)
    }
  },
  getters: {
    getModels(state){
      return state.aboutModels
    }
  },
  mutations: {
    setModels: (state, aboutModels)=> {
      state.aboutModels = aboutModels
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

//零件

import {mapActions, mapGetters} from "vuex";

export default {
  name: 'About',
  template: require('./about.template'),
  style: require('./about.style'),
  created () {
    document.title = 'About'
    this.findBy()
  },
  computed: mapGetters({
    abouts: 'getModels'
  }),
  methods: mapActions({
    findBy: 'findBy'
  })
}
Run Code Online (Sandbox Code Playgroud)

//视图

<div class="about" v-for="about in abouts">{{about.name}}</div>
Run Code Online (Sandbox Code Playgroud)

//错误

vue.js:2532[Vue warn]: Cannot use v-for on stateful component root element because it renders multiple elements:
<div class="about" v-for="about in abouts">{{about.name}}</div>

vue.js:2532[Vue warn]: Multiple root nodes returned from render function. Render function should return a single root node. (found in component <About>)
Run Code Online (Sandbox Code Playgroud)

Pri*_*ome 20

您正确映射Vuex状态的getter和action.您的问题是其他问题,因为您的错误消息指出...

在组件模板中,您不能v-for在根元素上使用指令.例如,这是不允许的,因为您的组件可以有多个根元素:

<template>
   <div class="about" v-for="about in abouts">{{about.name}}</div>
</template>
Run Code Online (Sandbox Code Playgroud)

而是这样做:

<template>
   <div>
      <div class="about" v-for="about in abouts">{{about.name}}</div>
   </div>
</template>
Run Code Online (Sandbox Code Playgroud)

***模板标签中的固定拼写错误**