vuejs之v-for

Posted 西西嘛呦

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vuejs之v-for相关的知识,希望对你有一定的参考价值。

看一个例子:

<html>

<head>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>

<body>
  <div id="app">
    <ul>
      <li v-for="(item, index) in arr">
        索引:{{index}},值:{{item}}
      </li>
    </ul>
    <ul>
      <li v-for="item in arr2">
        {{item.color}}
      </li>
    </ul>
    <input type="button" @click="add" value="添加颜色">
    <input type="button" @click="remove" value="移除颜色">
  </div>
  <script>
    var app = new Vue({
      el: \'#app\',
      data: {
        arr: [\'mike\', \'bob\', \'tom\', \'jack\'],
        arr2: [{ color: \'red\' }, { color: \'blue\' }, { color: \'yellow\' }],
      },
      methods: {
        add: function () {
      //添加一个元素 this.arr2.push({ color: \'black\' }); }, remove: function () {
      //移除左边第一个元素 this.arr2.shift(); }, }, })
</script> </body> </html>

效果:

点击添加颜色会在末尾添加一个black,点击删除颜色会删除掉第一个颜色:

说明:

v-for用于根据数据生成列表结构。

数组常和v-for使用。

语法是(item,index) in 数据。item是具体值,index是索引。

item和index可以结合其他指令一起使用。

以上是关于vuejs之v-for的主要内容,如果未能解决你的问题,请参考以下文章

VueJS:`v-for`不渲染元素

VueJS:在 v-for 之后触发 jQuery?

使用 v-for - vuejs 绑定本地图像

在 vuejs 中使用 v-for 时无限重复

如何在 vuejs 中打破 v-for 循环

如何专注于 vuejs 中的 v-for 中的输入