cncml手绘网

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

作者: admin    时间: 2018-7-4 10:56
标题: 循环语句
循环使用 v-for 指令。
v-for 指令需要以 site in sites 形式的特殊语法, sites 是源数据数组并且 site 是数组元素迭代的别名。
v-for 可以绑定数据到数组来渲染一个列表:
v-for 指令
1 o$ g2 j' o# ?/ p0 o7 P7 M
  1. <div id="app">: j$ V+ T+ P1 x$ i+ U* H& X- p0 h
  2.   <ol>5 h, t( C( b7 [% C8 e# N
  3.     <li v-for="site in sites">
    . P2 W0 I2 B* Z. |/ o) k8 c" @
  4.       {{ site.name }}
    : u2 k, U# g' P  a; d7 h
  5.     </li>
    * K( ^# R9 s# t3 s# g
  6.   </ol>" }) P5 k6 q% I' M5 U% M! F- @* n
  7. </div>
    ) \, S: P1 j* _
  8. 7 E) w  [7 j9 Y% m4 n0 z9 y! y
  9. <script>) ~. h9 I0 W3 E7 {: T4 M
  10. new Vue({" k* G2 U: P( Q; a
  11.   el: '#app',  H6 k( ]- s+ ?, K# t8 ]
  12.   data: {; N, u. c( T/ F% U( a- N$ F
  13.     sites: [1 H2 P1 v& j  s
  14.       { name: 'Runoob' },
    ! Y% l1 W/ m8 N1 D, p5 R
  15.       { name: 'Google' },
    3 ^" r3 q+ d9 m) m% T9 D
  16.       { name: 'Taobao' }% O% H$ X5 R4 k3 R, }( T
  17.     ]7 z' M# M1 e' |
  18.   }* m  d7 i7 D' ~  D/ j# @
  19. })5 }6 D0 e1 ^, y  G5 v" B7 P
  20. </script>
复制代码
. N6 d. {- I3 C. b. d! N+ m6 j' R

# k" g- {% ^/ Z& ]3 Y
模板中使用 v-for:
v-for
& k; ?) }1 N& a  c  ~
  1. <ul>
    1 z3 H9 r  @& \; Y) r- a/ u( |
  2.   <template v-for="site in sites">
    + I9 o6 o+ D1 D: b) u  e3 W
  3.     <li>{{ site.name }}</li>
    * z" Q6 M5 M2 \# Y
  4.     <li>--------------</li>
    % T' W+ I, q' i, V
  5.   </template>
    ' a* B, i7 a* n% l& L( U
  6. </ul>
复制代码
v-for 迭代对象
v-for 可以通过一个对象的属性来迭代数据:
v-for
& @3 S% D% r6 b' n
  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;"># r6 c+ X$ ~+ G2 q' S5 N7 Q. K
  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;">
    ) O8 M5 k: b. H2 U* g: f& Z$ b
  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;"># v8 l. S9 {9 q* G" Q& Q8 x3 i) q
  4.     {{ value }}8 _' c* z% R6 `. K0 Z: l% D
  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;">
    9 u( _- v/ @2 t9 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;">. P$ j" m1 t  Q' C
  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;">
    % E! X* N9 Z; Z# M
  8. 0 k- W* H. x& R; v6 ^2 r% n; {
  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;">
    ) {1 [  H3 H" U# I
  10. new Vue({! j4 n& b+ {2 x1 @- W* s
  11.   el: '#app',
    ( m# [; v6 V2 Y. G  l% n. i& Z* A
  12.   data: {' {; G$ y) g0 ]; s0 T' a! P! B9 r6 S
  13.     object: {
    4 n6 e+ M  S; w* N
  14.       name: '菜鸟教程',
    $ m8 V4 ]) `7 G; ?
  15.       url: 'http://www.runoob.com',6 f! J4 n  G0 E" N% s
  16.       slogan: '学的不仅是技术,更是梦想!'( p; e! \" R7 g+ n# F
  17.     }
    & t  l- i$ B/ l, b6 o' |
  18.   }- T8 k3 Z* y+ U" A0 u/ g( r
  19. })
    ; N# L2 L9 U( `: W2 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# j5 c6 q8 R/ N
6 l0 o9 A! B0 U/ q/ l5 ~8 X8 q
  1. <div id="app">
    ) p/ k2 i) F. u' U
  2.   <ul>1 M% z  U+ Y( \& {7 _
  3.     <li v-for="(value, key) in object">
    4 S7 M! y8 o& Q; d- Z
  4.     {{ key }} : {{ value }}; T. L5 J2 u5 T; |' T! ]
  5.     </li>
    % `* V5 _/ n( F( L( B9 a1 U
  6.   </ul>
    , h, Y) }' k2 S
  7. </div>
复制代码
第三个参数为索引:
v-for- T- u% Q! y# a+ q; z( X1 U

" Y$ D! D/ _" J8 \( b
  1. <div id="app">6 ~; ]# |( z) H% r  S. N' I8 V
  2.   <ul>. a% T4 s" M7 C) ?
  3.     <li v-for="(value, key, index) in object">0 S- R* a7 K% j/ g& v  v
  4.      {{ index }}. {{ key }} : {{ value }}
    : C# R( S8 w/ t# e
  5.     </li>
    ( l! z4 C( G& x8 W9 O4 M% t
  6.   </ul>- {9 d0 t: j: j7 B# n
  7. </div>
复制代码
v-for 迭代整数
v-for 也可以循环整数
v-for
. i, ~+ x- U- I. h) f* m. e& v$ i3 G4 |) C
  1. <div id="app">8 z7 `/ h* z8 h2 S* M
  2.   <ul>2 l& l) @; }! x3 k, {& e( C
  3.     <li v-for="n in 10">
    9 }# e4 U9 b5 P
  4.      {{ n }}
    5 r, K! y! H+ A3 m: o. T: {
  5.     </li>2 e  q: [$ p9 L. k! A
  6.   </ul>/ W( S) j. m( z' x. ?! M" v9 Z
  7. </div>
复制代码
! ~! J$ h% M7 h

( B3 E( R" z: l& R% u/ X
( e' l9 t* ~0 l+ ?! M, u# x/ a, @) P





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