cncml手绘网

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

作者: admin    时间: 2018-7-4 10:56
标题: 循环语句
循环使用 v-for 指令。
v-for 指令需要以 site in sites 形式的特殊语法, sites 是源数据数组并且 site 是数组元素迭代的别名。
v-for 可以绑定数据到数组来渲染一个列表:
v-for 指令) h3 T% q) V0 `. \% Y! s. G
  1. <div id="app">
    & d' @4 C6 E9 O- ]
  2.   <ol>
    0 p. Z$ r0 S( I, \
  3.     <li v-for="site in sites">8 V1 c2 N7 d# D: F, F8 P
  4.       {{ site.name }}
    ; u3 Y% a. L6 q$ \1 ^
  5.     </li>0 O6 p8 F, w' t; s
  6.   </ol>
    # @1 z0 Z' C: s" G/ F  ~2 h! R
  7. </div>
    , u8 k1 r( y% T, N
  8. + e2 F/ z2 {  s* E
  9. <script>: w, d. Q$ Y. l( [9 }6 t" w6 a
  10. new Vue({
    9 o* r+ M2 a+ V, e2 H8 f% E
  11.   el: '#app',: O* z4 M- G/ q) F, ?
  12.   data: {7 e. [( n- U" K+ d
  13.     sites: [$ \& ?6 U6 N9 V
  14.       { name: 'Runoob' },
    ) Z8 d; N9 O, |; z) L
  15.       { name: 'Google' },
    + V, N- D# M) n/ v$ }& o
  16.       { name: 'Taobao' }
    , \" f7 i( g$ ~( X( m6 J" a
  17.     ]
    $ B2 `+ ]5 B1 n2 o2 [( ]6 r
  18.   }) G. {5 ?* y! `0 l! B! [+ J( y) S
  19. })1 n2 {5 F" c( a5 l4 Z
  20. </script>
复制代码

6 _# }/ v! J2 A' d9 N4 G1 u) N6 k5 {6 U% t0 G
模板中使用 v-for:
v-for
" E: c& e: G: K5 `# v0 l
  1. <ul>0 v% t) E, k9 i- k3 H
  2.   <template v-for="site in sites">
    % j& I5 p. p6 A: ?) E
  3.     <li>{{ site.name }}</li>$ T' b2 T7 B. p. j
  4.     <li>--------------</li>3 j1 I. z1 \/ n5 f2 \
  5.   </template>7 a& M4 F) d8 K
  6. </ul>
复制代码
v-for 迭代对象
v-for 可以通过一个对象的属性来迭代数据:
v-for& g2 G9 e1 o/ \
  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;">
    / B5 B% F2 \( B# p2 G+ 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;">
    / H# r( Y1 O9 Q+ 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;"># B( r" a* |1 U0 S1 Q0 n4 [1 h8 C
  4.     {{ value }}
    5 v* S- `2 Y* p$ J
  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;">. N* O" a. j7 E% m
  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;">
    9 [- L8 p. K) W5 ]7 s  m
  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;">
    * p" T% d% U; e/ T% g. q# F5 p7 |2 \
  8. * W% x# ?2 L9 h% j+ p
  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;">/ l1 q1 Q. \5 l: E
  10. new Vue({
    ' n$ ]- P3 U- a
  11.   el: '#app'," B. r6 @! l1 X8 L# s% s) s5 Q. ^
  12.   data: {
    # V8 U( C- Q3 J
  13.     object: {
    " P2 @+ I/ W- k: v) H/ I
  14.       name: '菜鸟教程'," V# c* M0 d- T( ?2 ]
  15.       url: 'http://www.runoob.com',
    # d# J- L6 Z9 U
  16.       slogan: '学的不仅是技术,更是梦想!'
    ; _0 y2 @5 T+ ]6 X6 l& v8 Q$ p
  17.     }
    6 @$ X1 Y7 ?: D* _# w0 K8 P
  18.   }. Q) K! S3 J4 d4 t3 k# x# M
  19. }); D  Q9 @* Z6 Q# C$ A6 p0 O
  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
, Q/ W- U8 f6 J* Q
% Y' A5 z- Q: \1 r" Z; W% B
  1. <div id="app">& H, f+ g/ p8 y$ r& N3 p! c3 w
  2.   <ul>  }: `% m& R! ^/ V0 |8 C2 v7 X% U
  3.     <li v-for="(value, key) in object">8 i4 B- z6 D' m( T0 G( B- d8 W
  4.     {{ key }} : {{ value }}
    0 F' T9 R0 b; l3 v
  5.     </li>
    6 j' M8 t: v1 }) c
  6.   </ul>
    9 J# N! z# y  g
  7. </div>
复制代码
第三个参数为索引:
v-for6 u8 M( y5 _* z3 x
; \" a, u) G0 ^; d1 D- O
  1. <div id="app">+ G) @4 U4 W; B  X) T2 s" {+ g3 j7 \
  2.   <ul>( _/ ^. r9 I) Y5 ^# F. ~/ k. R; V
  3.     <li v-for="(value, key, index) in object">
    ' a# L: j! j7 G! F! D. X
  4.      {{ index }}. {{ key }} : {{ value }}3 C2 t. ^0 ~% b2 w2 g! |$ _
  5.     </li>% V7 X( ~; U$ z6 Z& `
  6.   </ul>
    . a+ V7 M3 n6 t" }( V
  7. </div>
复制代码
v-for 迭代整数
v-for 也可以循环整数
v-for) [7 \. d) \' X5 ], l5 ]: _
6 ]6 |" d+ v' k* T6 w7 R3 F2 k
  1. <div id="app">9 I# ?+ l2 C0 G
  2.   <ul>* ?$ C* E6 N$ E
  3.     <li v-for="n in 10">
    - ~" d! ?: y8 y5 H
  4.      {{ n }}
    4 B* B% L# Z5 S% x$ i# D/ c0 t
  5.     </li>* m4 `$ W- c7 [$ P# s
  6.   </ul>
    ! C- ^% ^3 y" h$ t; ?
  7. </div>
复制代码

" C  H; V! D: ^6 L/ ]/ a6 Z8 g6 Z" k& e; M* ~# h+ ^! v' |6 o2 [
, I2 l' a. Y) N% o

$ @( n0 R+ P8 M7 ?% T9 ?




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