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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[Vue.js] 循环语句

[复制链接]
跳转到指定楼层
楼主
发表于 2018-7-4 10:56:27 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
循环使用 v-for 指令。
v-for 指令需要以 site in sites 形式的特殊语法, sites 是源数据数组并且 site 是数组元素迭代的别名。
v-for 可以绑定数据到数组来渲染一个列表:
v-for 指令  y* m% h6 J8 h- j7 l( u
  1. <div id="app">
    1 \8 v& `7 C3 B% a- c" o9 x! C, r& s
  2.   <ol>
    % v6 J& N: L8 P' t" I# U! t
  3.     <li v-for="site in sites">
    $ a8 o* J' t$ C* y! V3 T! M- z
  4.       {{ site.name }}5 ?* K7 L* f* b2 Z* |' b& \1 {# o& Z
  5.     </li>3 w/ @* l+ F. X7 K$ u/ P
  6.   </ol>
    5 G5 ~( P  k; Q
  7. </div># ~% e8 [1 v" }( h$ P% O
  8. " K$ f8 G3 e5 M. S7 E$ J5 ^
  9. <script>$ ~$ n$ @$ r. o0 U: Y4 ~" L
  10. new Vue({
    ; G. M" G! y2 v6 E4 {# }: G
  11.   el: '#app',
    % O6 A* N3 R6 K
  12.   data: {
    - r; l: A5 E1 U2 e9 H7 v
  13.     sites: [- ?8 e: g* I' t/ ]* Z: l
  14.       { name: 'Runoob' },
    ; z3 l# {& Z# B. l( P# f% E
  15.       { name: 'Google' },
    3 D- o5 z4 S% x! X. z/ s+ {
  16.       { name: 'Taobao' }+ Z1 K+ O5 y2 Q2 g( U- O; q5 B, r0 x
  17.     ]- d8 E* n$ r4 z) o
  18.   }! j+ B! G& A! b' l. W: x7 n; T: L
  19. })) F6 b  R' {' y- V8 K9 K) P" V1 v
  20. </script>
复制代码
' e3 {& S+ S# \  a/ ]
* D- f. ~( F3 r4 ]8 r& k
模板中使用 v-for:
v-for, v+ n) e: V: J
  1. <ul>
    ) }, k& x1 o( l: E- |8 y
  2.   <template v-for="site in sites">" R% W7 I. L# j7 X" e0 y& i
  3.     <li>{{ site.name }}</li>/ @/ S1 A) I$ d! b+ i
  4.     <li>--------------</li>) b7 e8 d) V+ s/ A  J
  5.   </template>& g% \7 h0 r# u
  6. </ul>
复制代码
v-for 迭代对象
v-for 可以通过一个对象的属性来迭代数据:
v-for
8 e1 {% n* h  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;">
    # N# ]3 c( t9 U8 S  s. M
  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;">3 G9 ^5 n# ~) m* H. n( 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;">/ \8 z* T4 ^5 E
  4.     {{ value }}
    2 v) i) G3 i2 O/ f9 s4 |. 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# s+ R+ T! b% J. B8 b' l
  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;">
    ; [6 ~1 N3 r5 o% L6 y
  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;">
    ! m7 n9 m# d) N2 q* ?: Y

  8. 8 H+ _  R9 @9 ~1 M0 L
  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;">  x, ^  F' O$ V, F! k( Q
  10. new Vue({3 m( B# n% c/ g2 F1 c1 X
  11.   el: '#app',7 d' b3 [- G& d4 t( I
  12.   data: {' T7 |6 ^, N3 ]3 A# _- k: @
  13.     object: {
    0 E0 b! m9 ^/ X8 n8 G
  14.       name: '菜鸟教程',
    4 ?) `0 _+ H' a3 d, i3 Q* t, U
  15.       url: 'http://www.runoob.com',: [6 |  n7 Q/ ^3 O7 D) s0 }
  16.       slogan: '学的不仅是技术,更是梦想!'
    9 G, @5 h5 [3 l' Y" J3 \  Q
  17.     }4 \* N4 \' A: C, J; h* R
  18.   }: \( M' ~4 ?1 U: `6 |* }; @6 _9 z
  19. })  p; U$ o& c% C$ E! Y. x
  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
% V3 R2 J, c' {4 E9 Y9 G6 v- `8 b: k* l
  1. <div id="app">% ?1 ~& ?" N2 ~* [0 k7 i3 G
  2.   <ul>
    : ?5 H/ ^# d0 ^3 _8 c4 J
  3.     <li v-for="(value, key) in object">
    * u" q% S6 V) J/ H# y1 w: D
  4.     {{ key }} : {{ value }}
    4 n/ \( l# }- H7 v, x
  5.     </li>7 `7 N9 l$ f& u0 ~8 r
  6.   </ul>
      z: X2 ~8 U, v8 T+ W
  7. </div>
复制代码
第三个参数为索引:
v-for$ P8 w# i; e$ L( ]8 `+ g
' |! X" U9 K  B% U; s* y
  1. <div id="app">1 b1 y( c1 w1 v* i. g7 l1 Z
  2.   <ul>8 V7 R9 [& u) ~5 F7 b! r- r9 f& O
  3.     <li v-for="(value, key, index) in object">% i/ k, y% w! I% g5 X; y
  4.      {{ index }}. {{ key }} : {{ value }}
    & H4 X6 H& N1 l+ i; T# h2 M
  5.     </li>
    . p. \! }- Y3 g" \
  6.   </ul>
    8 x0 a& x- X4 A# _6 u9 t0 O0 V. X
  7. </div>
复制代码
v-for 迭代整数
v-for 也可以循环整数
v-for) r4 @5 R0 @: Y8 N# v: Q. W7 E7 Y
! }% z4 A4 E+ |! \' B2 n
  1. <div id="app">
    8 q8 s; k; `* Y. N" q" ~
  2.   <ul>/ D2 r7 ?0 T0 t! H
  3.     <li v-for="n in 10">" P* f$ l% ^, L# P1 [6 v2 b
  4.      {{ n }}3 G3 Y) w+ M- m# ?
  5.     </li>
    , X+ }/ Z% R# `
  6.   </ul>9 O5 C1 Z3 {$ c, U, P
  7. </div>
复制代码

0 _3 ?% o" @; Q- v$ k8 \% g- w' X! Y5 |4 {  l& g

3 N) i* F7 L- |$ F6 i3 A( n3 X$ t' [$ a9 C
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-3-16 20:28 , Processed in 0.059173 second(s), 19 queries .

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