cncml手绘网

标题: 循环语句 [打印本页]

作者: admin    时间: 2018-7-4 10:56
标题: 循环语句
循环使用 v-for 指令。
v-for 指令需要以 site in sites 形式的特殊语法, sites 是源数据数组并且 site 是数组元素迭代的别名。
v-for 可以绑定数据到数组来渲染一个列表:
v-for 指令2 p5 ~7 n$ w! p1 H$ [* F$ M2 `
  1. <div id="app">
    ; U2 }; K" z( }& R" P
  2.   <ol>
    : K' J% {; L; I, Z
  3.     <li v-for="site in sites">7 `& N1 i6 O; U, N
  4.       {{ site.name }}
    - g3 ^" \+ T2 m/ x# v8 O7 `" Y
  5.     </li>
    ( o6 \* ?0 m! o# Y  ]. }& V# W
  6.   </ol>3 p2 }8 H( N5 n1 |/ z# s3 J5 Y  I
  7. </div>+ u0 F2 y* h4 J+ N+ [: b

  8. : F( t# V0 X; C) `6 W2 a$ l
  9. <script>
    / l4 P1 T. R$ y
  10. new Vue({
    2 N4 v0 z% I7 }! n, z4 u
  11.   el: '#app',9 l% }+ h) ]9 S$ o3 A7 G
  12.   data: {$ Q3 ^  Y/ Y: C$ B" v
  13.     sites: [1 X1 p! K& q7 n- F% Q# Z
  14.       { name: 'Runoob' }," b* z, R$ r* g# L  i
  15.       { name: 'Google' },
    * B2 a+ w. N/ J" g" L) f- ]
  16.       { name: 'Taobao' }
    7 ]: ]" Z9 l; T) Z
  17.     ]! s  x. J- I. {: Q
  18.   }
    % ]* M) w/ X; t
  19. })
    5 }1 w2 L. ~- i7 ?
  20. </script>
复制代码

& }1 o* C! F( M  M$ T/ f" I4 k# [( N7 o6 J! ]( z$ {
模板中使用 v-for:
v-for
6 ?) I7 @- _5 P* v7 F
  1. <ul>/ @0 m8 m, n# O! m0 \9 T% n& q
  2.   <template v-for="site in sites">& a( A# p5 H; Z3 c
  3.     <li>{{ site.name }}</li>
    8 x2 d: V! E  D6 e7 G, f% t7 P
  4.     <li>--------------</li>
    & I6 g7 O$ d& D0 M
  5.   </template>( ~4 a/ E$ B( x5 E
  6. </ul>
复制代码
v-for 迭代对象
v-for 可以通过一个对象的属性来迭代数据:
v-for
9 H$ x% }2 @( t8 c9 V+ P2 N4 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;">  T% S5 @. I* I) X3 \. D& {- T
  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;">
    . }  I( ^' l* I2 P; H( o
  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;">
    ; m: U% N! _1 V
  4.     {{ value }}
    / Z5 U' z3 D0 f7 [- Z9 h0 ~
  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;">
    5 y. q* n  l9 B6 W, d
  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;">4 @5 l' ~9 k5 e) l
  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;">
    4 @( ]% K# O8 t  \, b+ P
  8. 2 w( s5 n. v" w7 M) f
  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;">
    8 }$ R) z. H5 ~; N% ^8 s
  10. new Vue({( }. ~( q# ]' v1 B  I5 }
  11.   el: '#app',5 V. Z0 i7 N) P" p
  12.   data: {9 }+ F3 z8 t& T
  13.     object: {
    ( V$ s; H8 @. t* W) j3 d
  14.       name: '菜鸟教程',
    / d7 B7 l  U/ r; U3 w1 Y
  15.       url: 'http://www.runoob.com',- d, d8 K* S7 s2 @' x1 E
  16.       slogan: '学的不仅是技术,更是梦想!'
    ; f* j* o- Z2 B/ x+ U; x3 c! u8 {
  17.     }4 p+ ?) Y5 F8 p1 r: _1 M
  18.   }) c& f' L+ ?3 Y0 u& F, T6 M
  19. }): S* x, G4 V7 ~3 F. D4 ?" K
  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-for3 n* G, R' [$ Z

' F* F4 q- w3 ~
  1. <div id="app">
    ) M; H% `" C. I" r6 [& ]
  2.   <ul># l0 `! P- H1 d' M; U, A1 ^; e
  3.     <li v-for="(value, key) in object">/ u: o2 Q) m# t; R
  4.     {{ key }} : {{ value }}- i9 X) }# W7 o$ O- s# p2 g8 ~  D
  5.     </li>' b7 G3 r+ L# D3 |: m3 f7 F; ~: t
  6.   </ul>: l, i. a$ D6 R  h0 f
  7. </div>
复制代码
第三个参数为索引:
v-for
, K) n% ?2 \- ]1 K* ]: I( b$ e% M$ y3 |% N
  1. <div id="app">6 K' K: V& I3 Z$ c- i4 j
  2.   <ul>
    7 f, d2 v  K/ O8 o4 l& ?! d
  3.     <li v-for="(value, key, index) in object">3 P0 w- L" w* u; \: k
  4.      {{ index }}. {{ key }} : {{ value }}
    ) `# g0 ~& C) W6 i4 K
  5.     </li>* {( A5 T7 J7 i0 u
  6.   </ul>
    . O  \6 h" A  r/ `
  7. </div>
复制代码
v-for 迭代整数
v-for 也可以循环整数
v-for$ {4 k: ]& m# b+ G4 b- [
+ V4 A! |3 M( |% H& q
  1. <div id="app">
    4 O9 z& _. _; f# C# z9 t5 x- E: @
  2.   <ul>
    5 S; Q$ r  M; `" a
  3.     <li v-for="n in 10">
    ( F1 Y) n* n& L2 @0 j
  4.      {{ n }}
    1 b: x. Y  D6 A/ O
  5.     </li>, T' d# z" |. T4 Z2 x
  6.   </ul>
    ' c4 P9 X& v3 X3 q6 {
  7. </div>
复制代码
2 M' q9 R$ g; U" c

9 T1 i$ U( G6 [  {
$ d* L5 x: m# B! B) B3 J1 s% j8 g) |& K+ d; j  \& g; w7 i4 w





欢迎光临 cncml手绘网 (http://bbs.cncml.com/) Powered by Discuz! X3.2