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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[Vue.js] 循环语句

[复制链接]
跳转到指定楼层
楼主
发表于 2018-7-4 10:56:27 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
循环使用 v-for 指令。
v-for 指令需要以 site in sites 形式的特殊语法, sites 是源数据数组并且 site 是数组元素迭代的别名。
v-for 可以绑定数据到数组来渲染一个列表:
v-for 指令
8 y$ a7 k& d" _& x
  1. <div id="app">
    - @9 Z; J+ k* X- p
  2.   <ol>  a7 {: b8 E0 v) B: q+ a: f" ?
  3.     <li v-for="site in sites">
    , N: v# w* c8 d8 K2 e. ]4 Q
  4.       {{ site.name }}9 M( M2 K# j) k( N
  5.     </li>( @* f6 `5 P# `- D8 E; O. T- N
  6.   </ol>
    7 @- P; R( O  I0 R* |( v
  7. </div>/ O+ a7 X% D1 q. H! M

  8. 0 d/ g# i+ E4 S8 O$ O7 u
  9. <script>7 V0 M' T8 r  m9 Q' g) g4 L
  10. new Vue({
    ) c! Q8 ?' o) P
  11.   el: '#app',6 U! L/ h( W# K5 b, a9 X9 `; ]8 e
  12.   data: {
    : W5 m0 K" w1 C0 b) s1 j! g0 a
  13.     sites: [5 P8 F$ {: D3 E; Q
  14.       { name: 'Runoob' },
      v$ Y5 y6 G% N- h9 }6 g
  15.       { name: 'Google' },
    ) O/ s1 @, ~6 L, q( R+ [, s2 ]
  16.       { name: 'Taobao' }' I( x9 o" d: F0 X, c0 o! m5 V
  17.     ]
    ( W. K7 O& q3 p! R* Q" g" V) c
  18.   }
    + p! u; I9 y2 G( V  {% O
  19. })
    # p+ E) S0 ?% p, B
  20. </script>
复制代码

! a; B" k( _( Y9 e! t
9 K9 u' N8 @. }+ B! k5 S
模板中使用 v-for:
v-for2 S' W1 o; p" z7 d6 h
  1. <ul>
    2 v+ A( d, f  ]
  2.   <template v-for="site in sites">
    . u8 O* N2 {5 G( b& F! W  a
  3.     <li>{{ site.name }}</li>
    ' @: [& M, X% E# s6 H" w& ?9 e# \8 d
  4.     <li>--------------</li>
    4 s" Y3 u: j- p( }  ]
  5.   </template>! H) {+ Z2 h  ^% V0 Q
  6. </ul>
复制代码
v-for 迭代对象
v-for 可以通过一个对象的属性来迭代数据:
v-for
* H9 t# Q9 A- ]% i
  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;">, I  L$ U% Y+ O. I' [% l9 N
  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;">. Q$ G& F0 J* {3 H/ G' W. M
  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;">
    . m1 F4 A% ~0 O* ^6 }- @
  4.     {{ value }}
    $ `( D" f5 Y1 b* I
  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;">
    ; Z& b. Q  {5 ]4 C) M! x' B6 }
  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;">% P- E- f3 h" T7 h4 ]* W1 I* s
  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;">
    ! w4 l9 m& T) b4 w
  8. , X5 h+ J  L. k( j" y( |" E
  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;">
    ( }2 K) ^% K5 S. T4 f
  10. new Vue({
    , d6 c' E7 W1 v8 O( P' N# h' ~2 ^
  11.   el: '#app',
    # T  a4 o- ~5 y( m, y0 |2 Y
  12.   data: {$ [. C# ?0 H- J- g
  13.     object: {
    1 g+ w+ K: P+ c0 F1 U
  14.       name: '菜鸟教程',1 _9 C& c# A1 |
  15.       url: 'http://www.runoob.com',
    1 p& U7 ?3 r0 ~4 ~$ K, J
  16.       slogan: '学的不仅是技术,更是梦想!'
    # d+ {  [, ]& P, l. T. h
  17.     }
    # Q  }) b3 u9 C$ w* @( M
  18.   }
    ( V5 G, c, W) A  d& J
  19. })
    + h* @# `+ E+ B( k' P3 J# g2 q1 W
  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' a7 @  p8 B; ^1 V* R! B; I' h

- u; ]7 Z9 X* R6 W  o# Y: e
  1. <div id="app">
    * A0 @. E) w. G: ~' ?
  2.   <ul>6 K0 C. v. y3 G6 s$ V0 T' P
  3.     <li v-for="(value, key) in object">- c8 B% ~3 m1 j8 a; N' ^, k4 N
  4.     {{ key }} : {{ value }}
    . M( \; B. K! Q$ s1 S8 X
  5.     </li>; H+ x) a4 Y. \. r' S! u8 L9 w0 I
  6.   </ul>0 t( ^0 X1 A5 ^) B8 ~7 Q* G% X
  7. </div>
复制代码
第三个参数为索引:
v-for9 M4 d* `( Y* p6 d# g2 V. c

/ k1 X/ I+ V5 a$ v
  1. <div id="app">
    ; z5 ]8 E) [- z$ O2 z& R5 I
  2.   <ul>8 |# G. p( f/ N7 T0 S( `
  3.     <li v-for="(value, key, index) in object">1 i9 j* p& V: ]( Q
  4.      {{ index }}. {{ key }} : {{ value }}' X- `4 L! i, y: E0 Y3 F3 n" {7 `
  5.     </li>
    0 \' @) `$ e# q/ H+ `- |) c1 D
  6.   </ul>
    0 G) o# e; D( Y8 q* i) `$ n) u
  7. </div>
复制代码
v-for 迭代整数
v-for 也可以循环整数
v-for) U, j9 ?" {7 Z" T7 f) {
% W3 V* e8 l; q, b
  1. <div id="app">
    , f$ G7 v9 c' q2 y! }
  2.   <ul>' X: c7 ~& W4 V( F9 T% w  h
  3.     <li v-for="n in 10"># Q6 L( i5 v% h- `) z& c
  4.      {{ n }}
    , `/ u1 M/ Y% Q! D
  5.     </li>
    & q. K! c3 w- [
  6.   </ul>
    % n, W, h4 W0 O' d; T& N! p8 V
  7. </div>
复制代码
# j$ q. I' q) G
, G) W  Y; d; }! M& f' Z* P) N! u

8 X% c" e5 w! ~) c) S  G) t# [0 u2 U. Z; I) |5 e
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2024-12-22 21:04 , Processed in 0.150537 second(s), 19 queries .

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