9

//お店

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>)
4

1 に答える 1

20

Vuex 状態ゲッターとアクションを正しくマッピングしています。エラーメッセージが示すように、あなたの問題は別のものです...

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>

** *テンプレートタグのタイプミスを修正 **

于 2016-09-18T13:29:01.190 に答える