如何创建n个div,n个来自Vue中的数据?

时间:2018-11-27 17:26:28

标签: vue.js vuejs2

此代码不会创建4个div。它只是创建一个div,其中包含4。如何通过从数据中获取数字来创建div?

<template>
  <div>
    <div v-for="n in number" :key="n">
      {{n}}
    </div>
  </div>
</template>

<script>
export default {
  data: function () {
    return{
      number: 4
    }
  }
}
</script>

1 个答案:

答案 0 :(得分:1)

v-for可以确定整数范围:https://vuejs.org/v2/guide/list.html#v-for-with-a-Range

这是您的div示例:
https://codesandbox.io/s/l7lr4q6qoz

如果您的版本不起作用,则问题出在其他地方,而不是v-for="n in 5"