您尚未登录,请登录后浏览更多内容! 登录 | 立即注册

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10788|回复: 0
打印 上一主题 下一主题

[Vue.js] 循环语句

[复制链接]
跳转到指定楼层
楼主
发表于 2018-7-4 10:56:27 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
循环使用 v-for 指令。
v-for 指令需要以 site in sites 形式的特殊语法, sites 是源数据数组并且 site 是数组元素迭代的别名。
v-for 可以绑定数据到数组来渲染一个列表:
v-for 指令: R5 b% k# }% v2 c" C
  1. <div id="app">1 l" k1 M4 A. G3 |& ~
  2.   <ol>
    , D# i& u+ {) k- }6 r  c
  3.     <li v-for="site in sites">
    3 [. D- R: x7 D' h4 b
  4.       {{ site.name }}
    % f' s& V  V6 y" U! H* E
  5.     </li>  I' b# ~' P* i
  6.   </ol>1 z! q. @3 z; N2 i2 M1 V
  7. </div>
    0 Q! P( V5 j" @

  8. 2 Z: f$ w# M+ T6 ]7 {
  9. <script># C) B* [. X) c( m( s' f
  10. new Vue({
    $ J4 {) R! h" ^% e8 u
  11.   el: '#app'," }6 r6 s) I+ g$ G" O4 a
  12.   data: {
    6 w- f: Y! _6 {  ]- J  |! k
  13.     sites: [+ w; X# a8 q% X. W/ Q9 P, H7 E
  14.       { name: 'Runoob' },9 E& @$ t2 {$ T4 U" c$ G
  15.       { name: 'Google' },& V. Y) H' F6 f/ b3 s
  16.       { name: 'Taobao' }
    % p! Y" g2 ?; F
  17.     ]
    " ]( o2 v, s0 h1 y1 a  a
  18.   }0 }9 c1 Y0 C! \, s2 Y
  19. })
    & K- Y7 t7 r5 _- W# U: J" A
  20. </script>
复制代码

7 K& ]1 s5 t1 Y0 P
  T  [* z: e7 n: c4 J- v, y3 v# _# u
模板中使用 v-for:
v-for
. k7 k9 s$ l5 y/ S: }7 P/ |
  1. <ul>
    ( s  R$ }' x" K1 L
  2.   <template v-for="site in sites">
    3 C( M) M7 U; |9 M  W3 d/ G; D
  3.     <li>{{ site.name }}</li>+ y* p% ?+ D0 q, |
  4.     <li>--------------</li>- J& z; S3 p3 v* W3 o$ j
  5.   </template>, M/ R/ l7 O$ X6 ~: W* j
  6. </ul>
复制代码
v-for 迭代对象
v-for 可以通过一个对象的属性来迭代数据:
v-for
0 Z& `. L" O5 M& M$ N9 J- K
  1. <div><span class="hl-brackets" style="border: 0px; color: olive; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;"><</span><span class="hl-reserved" style="border: 0px; color: green; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">div</span><span class="hl-code" style="border: 0px; color: gray; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;"> </span><span class="hl-var" style="border: 0px; color: rgb(0, 0, 139); font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">id</span><span class="hl-code" style="border: 0px; color: gray; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">=</span><span class="hl-quotes" style="border: 0px; color: rgb(139, 0, 0); font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">"</span><span class="hl-string" style="border: 0px; color: rgb(170, 17, 17); font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">app</span><span class="hl-quotes" style="border: 0px; color: rgb(139, 0, 0); font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">"</span><span class="hl-brackets" style="border: 0px; color: olive; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">></span><span class="hl-code" style="border: 0px; color: gray; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">
    1 i" m. i( h- y( M/ x8 V
  2.   </span><span class="hl-brackets" style="border: 0px; color: olive; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;"><</span><span class="hl-reserved" style="border: 0px; color: green; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">ul</span><span class="hl-brackets" style="border: 0px; color: olive; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">></span><span class="hl-code" style="border: 0px; color: gray; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">$ ]$ W  a+ L% q( B4 T% m, E" o' J
  3.     </span><span class="hl-brackets" style="border: 0px; color: olive; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;"><</span><span class="hl-reserved" style="border: 0px; color: green; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">li</span><span class="hl-code" style="border: 0px; color: gray; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;"> </span><span class="hl-var" style="border: 0px; color: rgb(0, 0, 139); font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">v-for</span><span class="hl-code" style="border: 0px; color: gray; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">=</span><span class="hl-quotes" style="border: 0px; color: rgb(139, 0, 0); font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">"</span><span class="hl-string" style="border: 0px; color: rgb(170, 17, 17); font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">value in object</span><span class="hl-quotes" style="border: 0px; color: rgb(139, 0, 0); font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">"</span><span class="hl-brackets" style="border: 0px; color: olive; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">></span><span class="hl-code" style="border: 0px; color: gray; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">% X- l$ q5 T( ~/ X9 X3 J
  4.     {{ value }}
    # w4 p( A6 M, G2 t% o  J+ J
  5.     </span><span class="hl-brackets" style="border: 0px; color: olive; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;"></</span><span class="hl-reserved" style="border: 0px; color: green; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">li</span><span class="hl-brackets" style="border: 0px; color: olive; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">></span><span class="hl-code" style="border: 0px; color: gray; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">
    2 V. y* B( M0 l7 q' J7 e0 m0 ^
  6.   </span><span class="hl-brackets" style="border: 0px; color: olive; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;"></</span><span class="hl-reserved" style="border: 0px; color: green; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">ul</span><span class="hl-brackets" style="border: 0px; color: olive; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">></span><span class="hl-code" style="border: 0px; color: gray; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">2 M9 G5 a: C& N# x
  7. </span><span class="hl-brackets" style="border: 0px; color: olive; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;"></</span><span class="hl-reserved" style="border: 0px; color: green; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">div</span><span class="hl-brackets" style="border: 0px; color: olive; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">></span><span class="hl-code" style="border: 0px; color: gray; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">
      O! ^% L0 K& ]5 K

  8. $ t3 x8 r0 i7 d9 H; I# R( p6 [7 ?
  9. </span><span class="hl-brackets" style="border: 0px; color: olive; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;"><</span><span class="hl-reserved" style="border: 0px; color: green; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">script</span><span class="hl-brackets" style="border: 0px; color: olive; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">></span><span class="hl-code" style="border: 0px; color: gray; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">
    / @; Z" B; }6 e) V
  10. new Vue({0 c6 S) B# f* f/ H; F2 A+ H) A8 d
  11.   el: '#app',. {8 c& X  d2 G' x8 Y" Y* T
  12.   data: {1 z5 q! ^' `1 ]2 m' \- j6 J
  13.     object: {/ d& n+ b( w9 }" I2 V. z7 K
  14.       name: '菜鸟教程',) u8 D) E4 w4 j1 t* k# Z; |
  15.       url: 'http://www.runoob.com',5 M! ?0 [- q" o- Z& _, @! T
  16.       slogan: '学的不仅是技术,更是梦想!'2 T8 J/ ~7 \. V6 H+ v
  17.     }
    ; t4 l8 I. |3 C+ B
  18.   }  q1 @& [! j8 s0 G$ _, W
  19. })
    - g9 `+ {% b$ G
  20. </span><span class="hl-brackets" style="border: 0px; color: olive; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;"></</span><span class="hl-reserved" style="border: 0px; color: green; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">script</span><span class="hl-brackets" style="border: 0px; color: olive; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 13.2px; white-space: pre-wrap;">></span></div><div></div>
复制代码
你也可以提供第二个的参数为键名:
v-for
6 A, ]0 {/ n2 H+ q
8 Y& Y5 o, E4 I2 N" K  C
  1. <div id="app">
    , ?0 M* Z( j  B' I
  2.   <ul>
    $ n2 \- T/ W/ V, d8 d+ j. ^
  3.     <li v-for="(value, key) in object">; k) [( B' d7 z8 F5 t! ^0 G
  4.     {{ key }} : {{ value }}; j+ l( R; c2 L& m  X
  5.     </li>
    ; |. R3 N9 I! S6 Y2 \# c
  6.   </ul>
    3 N5 V4 ^; q$ `8 P+ G) `: p
  7. </div>
复制代码
第三个参数为索引:
v-for
: B3 w+ }8 R0 x+ }8 G: _) l# g
/ l0 F8 K" A8 p. y- m
  1. <div id="app">
    ) H: Y: F7 h+ n1 K2 `6 u3 ?
  2.   <ul>$ k  A& I$ }9 p
  3.     <li v-for="(value, key, index) in object">
      {; [8 ?1 K' i6 _# R
  4.      {{ index }}. {{ key }} : {{ value }}. [& Y8 G( I) O6 ^6 R0 x1 a8 h" Z: T* z
  5.     </li>
    4 `9 z7 C4 c' e/ F8 d: {6 N8 E
  6.   </ul>
    , h6 e* F7 y# h' q# k
  7. </div>
复制代码
v-for 迭代整数
v-for 也可以循环整数
v-for" e& b! B0 W% p6 ^" x# `6 J
  p3 y1 v% x1 Y1 [& n0 {1 S
  1. <div id="app">
    % J& f4 a  x% F+ x! b8 ]
  2.   <ul>
    4 |# t  D" ]2 g6 F. |
  3.     <li v-for="n in 10">' r" ]5 G% x) m& I
  4.      {{ n }}
    8 |2 |; z6 L. w- }
  5.     </li>
    ! V& \. l7 `) @
  6.   </ul>
    2 w2 H7 W$ M! O3 j% f$ G7 R& T
  7. </div>
复制代码

% A3 `6 w7 g3 @! h  z
8 Y/ a" F( `/ L, f) a2 p' U( |( Y4 v* z8 ?

2 O- k. L* b3 Q& F
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-8-4 12:40 , Processed in 0.052732 second(s), 20 queries .

Copyright © 2001-2026 Powered by cncml! X3.2. Theme By cncml!