//店铺

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
    }
  }
}

//成分
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'
  })
}

//看法
<div class="about" v-for="about in abouts">{{about.name}}</div>

//错误
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>)

最佳答案

您正在正确映射Vuex状态 getter 和操作。您的问题是错误消息指出的其他问题...

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

<template>
   <div class="about" v-for="about in abouts">{{about.name}}</div>
</template>

而是这样做:
<template>
   <div>
      <div class="about" v-for="about in abouts">{{about.name}}</div>
   </div>
</template>

** *固定模板标签中的错字**

关于vue.js - Vue : How to use store with component?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39554692/

10-11 11:47