7.Vue2-循环语句的用法

题记

vue2循环语句的用法

v-for

绑定数组
html 复制代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>实例</title>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
  <ol>
    <li v-for="site in sites">
      {{ site.name }}
    </li>
  </ol>
</div>

<script>
new Vue({
  el: '#app',
  data: {
    sites: [
      { name: 'ng1' },
      { name: 'ng2' },
      { name: 'ng3' }
    ]
  }
})
</script>
</body>
</html>

实例:

html 复制代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>实例</title>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
  <ul>
    <template v-for="site in sites">
      <li>{{ site.name }}</li>
      <li>--------------</li>
    </template>
  </ul>
</div>

<script>
new Vue({
  el: '#app',
  data: {
    sites: [
      { name: 'ng1' },
      { name: 'ng2' },
      { name: 'ng3' }
    ]
  }
})
</script>
</body>
</html>
v-for迭代对象
html 复制代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>实例</title>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
  <ul>
    <li v-for="value in object">
    {{ value }}
    </li>
  </ul>
</div>

<script>
new Vue({
  el: '#app',
  data: {
    object: {
      name: '111',
      url: '222',
      slogan: '333'
    }
  }
})
</script>
</body>
</html>
键名
html 复制代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>实例</title>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
  <ul>
    <li v-for="(value, key) in object">
    {{ key }} : {{ value }}
    </li>
  </ul>
</div>

<script>
new Vue({
  el: '#app',
  data: {
    object: {
      name: '111',
      url: '222',
      slogan: '333'
    }
  }
})
</script>
</body>
</html>
索引
html 复制代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>实例</title>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
  <ul>
    <li v-for="(value, key, index) in object">
     {{ index }}. {{ key }} : {{ value }}
    </li>
  </ul>
</div>

<script>
new Vue({
  el: '#app',
  data: {
    object: {
      name: '111',
      url: '222',
      slogan: '333'
    }
  }
})
</script>
</body>
</html>
v-for迭代整数
html 复制代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>实例</title>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
  <ul>
    <li v-for="n in 10">
     {{ n }}
    </li>
  </ul>
</div>

<script>
new Vue({
  el: '#app'
})
</script>
</body>
</html>

后记

觉得有用可以点赞或收藏!

相关推荐
大怪v7 小时前
AI抢饭?前端佬:我要验牌!
前端·人工智能·程序员
新酱爱学习7 小时前
字节外包一年,我的技术成长之路
前端·程序员·年终总结
小兵张健7 小时前
开源 playwright-pool 会话池来了
前端·javascript·github
IT_陈寒10 小时前
Python开发者必知的5大性能陷阱:90%的人都踩过的坑!
前端·人工智能·后端
codingWhat11 小时前
介绍一个手势识别库——AlloyFinger
前端·javascript·vue.js
Lee川11 小时前
深度拆解:基于面向对象思维的“就地编辑”组件全模块解析
javascript·架构
代码老中医11 小时前
2026年CSS彻底疯了:这6个新特性让我删掉了三分之一JS代码
前端
进击的尘埃11 小时前
Web Worker 与 OffscreenCanvas:把主线程从重活里解放出来
javascript
不会敲代码111 小时前
Zustand:轻量级状态管理,从入门到实践
前端·typescript
踩着两条虫11 小时前
VTJ.PRO 双向代码转换原理揭秘
前端·vue.js·人工智能