如何在我的组件文件中嵌套 v-fors?

How can I nest v-fors inside my component file?

我已经阅读了 VueJS 教程,但我仍然无法想出解决方案。

我有一个列表列表,我想用手风琴来显示它们(这是一个来自 vue-strap 的组件,之前测试过几次都可以正常工作)。

所以有一个列表,例如:

'myList': [
  ['el 1', 'el 2', 'el 3'], ['el 1', 'el 2'], ['el another']
]

我希望得到以下可视化效果:

列表 1:

列表 2:

列表 3:

但是 VueJS 没有渲染这个组件...!

代码如下:

<template>
  <accordion id="rabo" :one-at-atime="true">
    <template v-for="list in myLists">
      <panel header="List #{{ $index }}" :is-open="true">
        <ul>
          <li v-for="el in list">
            {{el}}
          </li>
        </ul>
      </panel>
    </template>
  </accordion>
</template>

<style lang="scss" scoped>
</style>

<script>
  import Vue from 'vue'
  import { accordion, panel } from 'vue-strap'

  module.exports = {
    components: {
      'accordion': accordion,
      'panel': panel
    }
  }

  new Vue({
    el: '#rabo',
    data: {
      'myLists': [['el 1', 'el 2', 'el 3'],['el 1','el 2'],['el another']]
    }
  })
</script>

你应该:

  1. 将 Vue 实例创建到单独的文件
  2. myLists数组放入组件data
  3. 绑定 header 道具

MyAccordion.vue

<template>
  <accordion :one-at-atime="true">
    <template v-for="list in myLists">
      <panel :header="`List #${$index}`" :is-open="true">
        <ul>
          <li v-for="el in list">
            {{el}}
          </li>
        </ul>
      </panel>
    </template>
  </accordion>
</template>

<script>
  import { accordion, panel } from 'vue-strap'

  export default {
    components: {
      accordion, panel
    },

    data () {
      return {
        myLists: [
          ['el 1', 'el 2', 'el 3'],
          ['el 1', 'el 2'],
          ['el another']
        ]
      }
    }
  }
</script>

Vue 实例

import Vue from 'vue'
import MyAccordion from './MyAccordion.vue'

new Vue({
  el: '#demo',
  components: { MyAccordion }
})

http://www.webpackbin.com/VyPHjF_V-