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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[Vue.js] 循环语句

[复制链接]
跳转到指定楼层
楼主
发表于 2018-7-4 10:56:27 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
循环使用 v-for 指令。
v-for 指令需要以 site in sites 形式的特殊语法, sites 是源数据数组并且 site 是数组元素迭代的别名。
v-for 可以绑定数据到数组来渲染一个列表:
v-for 指令
6 O' K  ?: |" z0 i2 V4 y$ p# e$ N( G
  1. <div id="app">7 W4 x6 e9 |. d7 X/ z7 J
  2.   <ol>. m% j% x" T5 T3 s8 s/ H+ R4 Z- b
  3.     <li v-for="site in sites">4 P/ V/ G5 v- y. a1 v# P
  4.       {{ site.name }}
    0 q  A+ {+ q: {( L2 U8 D( U$ @
  5.     </li>
    6 O: ~+ `$ X+ T5 Q; a* ^; g+ N0 D
  6.   </ol>
    4 _1 f) x: c' H" y; R; o- I
  7. </div>: b; T( N1 |3 ]3 H' B1 d) I

  8. - t2 R, |' v8 k! ]& @
  9. <script>
    ) M: }( h  F' |3 S  w1 |1 j, |
  10. new Vue({; k, Y. @* M7 n" p, K. S+ F
  11.   el: '#app',
    % Q3 `6 p0 j6 s5 L9 ?  b3 U
  12.   data: {
    7 ]/ Y; G8 \# ?" f8 x& H/ t
  13.     sites: [
    4 I& Z6 C# ~% w1 U/ @6 E: \
  14.       { name: 'Runoob' },
    4 n8 [( _  N( F. K) @
  15.       { name: 'Google' },
    6 A! s- Y. U# I3 C" v, k3 H
  16.       { name: 'Taobao' }3 c' \8 S( f# W( m: h) m7 M" X
  17.     ]
    0 l: B: v& T5 Z
  18.   }# d$ ?/ {* t& I2 F! @7 w! R! f
  19. })4 ~5 F6 ^% H0 L4 i
  20. </script>
复制代码
2 m0 W' M* j% R' G0 W

# d- l  U; t5 e4 R
模板中使用 v-for:
v-for# T1 z3 g/ I6 U3 V5 a* k4 }. h  t+ c
  1. <ul>
    & P$ z" e  t* i( L! Q, C6 U
  2.   <template v-for="site in sites">
    & m1 u9 J8 ^" P+ w9 N
  3.     <li>{{ site.name }}</li>
    % `6 b" F3 |& c4 L
  4.     <li>--------------</li>
    2 w; G- [4 W5 X4 l- Z/ P6 A
  5.   </template>5 T- s6 [" K, Z6 C  r" z. f9 \+ ]
  6. </ul>
复制代码
v-for 迭代对象
v-for 可以通过一个对象的属性来迭代数据:
v-for
/ n$ d5 m1 t7 Y
  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;">( {4 ?# V9 M; a* c+ z$ ?4 E
  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;">/ _6 B2 S( @0 J6 j2 r0 k4 k
  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;">
    ( U! g& {- l. i& c- \% A
  4.     {{ value }}
    ) K; \& Z7 c# D3 j1 ^! C
  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;">
    - \) k. a; g8 {! S+ q" q* U' H' T0 c
  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;">  c$ ^8 E. ~* P+ ]6 A; N
  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;">
    . S& R( Y+ P2 l# |0 `

  8. + c3 B* _8 o+ x
  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;">
    - }3 W6 b4 X8 B
  10. new Vue({9 X$ R) g9 e+ t( e) e  z& i
  11.   el: '#app',
    : f) V+ _7 K% D  ?0 F: E
  12.   data: {
    . _6 S5 ^# h, o8 i9 j5 f. z9 L+ v
  13.     object: {
    3 f' z1 s: u3 a9 @% u* Z# I
  14.       name: '菜鸟教程',
    * F3 a* g! |0 y0 }: G* C
  15.       url: 'http://www.runoob.com',
    ( E+ T; H1 T" E( i/ r, W
  16.       slogan: '学的不仅是技术,更是梦想!'
    5 d) ?  p- ]; K& j7 P) U
  17.     }
    5 X7 E* ~* m* A+ v7 S# l
  18.   }; ^  Q+ ]- A2 g+ Q
  19. })+ Z0 U/ Z+ u$ D- v
  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
) d. x+ X' K. Z4 Y/ j7 }7 S
# T# {1 Y1 k+ R2 \5 p  S" f1 e
  1. <div id="app">! I5 K, A0 G' K+ E
  2.   <ul>7 {3 M! V! @6 j  a4 p# U% V
  3.     <li v-for="(value, key) in object">- \5 j' }4 R4 |# U( Z1 e
  4.     {{ key }} : {{ value }}) I. a8 U' ?6 {* q
  5.     </li>) Z7 K& ?, D% {
  6.   </ul>' o; I+ g/ l" w" j6 R
  7. </div>
复制代码
第三个参数为索引:
v-for
' B9 s3 G1 ?$ P4 L  _* A9 M! r1 G3 U
; i& L9 [; j6 N% q! d* Y
  1. <div id="app">! z& g* r) {8 N& J
  2.   <ul>
    4 {7 [9 ^8 O! h( B3 M1 w6 w" L
  3.     <li v-for="(value, key, index) in object">
    " W& t- ^+ I: ^! a. k/ @
  4.      {{ index }}. {{ key }} : {{ value }}  Y% Q( j( e* H5 p/ M: x5 G
  5.     </li>
    2 b& T1 m$ q" X& k% t
  6.   </ul>
    4 j, U% U* a" ?3 c
  7. </div>
复制代码
v-for 迭代整数
v-for 也可以循环整数
v-for% ^" m7 N9 }: @* w+ E9 j
; a  j0 x4 l5 V% \6 e
  1. <div id="app">
    + E2 p: ?  Z1 U: t+ C0 [1 T
  2.   <ul>
    8 H- t, E9 I$ j/ b. F2 E" W0 }" s
  3.     <li v-for="n in 10">
    ) G) \- V$ Y! N
  4.      {{ n }}6 }) q; y+ x+ s$ f
  5.     </li>
    ! j: Y0 @" S7 d: V
  6.   </ul>- a6 b8 y. P3 ?
  7. </div>
复制代码
; m; G$ f1 }7 l
, D4 G. n" D: V. \, {- ^" p

) g) s0 ]; A: m) @  W9 N$ f) }: Y6 _, z& g3 N* E
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2024-12-22 16:29 , Processed in 0.105806 second(s), 19 queries .

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