cncml手绘网

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

作者: admin    时间: 2018-7-4 10:56
标题: 循环语句
循环使用 v-for 指令。
v-for 指令需要以 site in sites 形式的特殊语法, sites 是源数据数组并且 site 是数组元素迭代的别名。
v-for 可以绑定数据到数组来渲染一个列表:
v-for 指令
4 b1 ?$ t) K. {
  1. <div id="app">
    7 R9 r& ]5 H' m7 a. B+ }
  2.   <ol>
    ( \" _6 r) @* b
  3.     <li v-for="site in sites">
    3 f' V1 j4 [6 o7 J! c3 O# p
  4.       {{ site.name }}$ Z0 G1 J6 R2 A* `& j6 j
  5.     </li>
    8 P: V: m. z0 |3 C( P( F
  6.   </ol>5 ~% V8 r4 v6 c0 y% W4 E5 L- a5 G
  7. </div>
    3 v, a6 F+ J5 `6 z* N; s

  8. * I# b: F# M# L2 C7 N; S  M4 ]
  9. <script>" b! V# j' D5 J4 N5 o
  10. new Vue({; h9 w) B: x+ r( I
  11.   el: '#app',
    + C% K" a9 G: k0 ?% j
  12.   data: {4 ?+ D: {7 h3 p8 `
  13.     sites: [
    & @: b( B. S5 g# `* ^  [
  14.       { name: 'Runoob' },& J% t0 N* q, O$ `$ ?" `, n
  15.       { name: 'Google' },
    / H: O# Z5 [6 n% N5 b  ?4 l
  16.       { name: 'Taobao' }- ?5 y, b2 r+ o
  17.     ]
    / F" M# M9 v# S' n
  18.   }
    & S  d# D9 _- _% t4 N! \! k
  19. })
    + |1 v1 J/ r) t: z- |2 ?4 N
  20. </script>
复制代码
# c% \8 }. ?( g9 D4 R% j1 s9 w1 S
$ Q; q5 x2 }. M. g: H% B
模板中使用 v-for:
v-for* Y" F. a) ~, E
  1. <ul>; j! e- C# h/ _
  2.   <template v-for="site in sites">$ q6 {+ L6 |) |$ C0 n
  3.     <li>{{ site.name }}</li>
    + l; ~6 _9 r; h$ ?5 K' F% j
  4.     <li>--------------</li>; \: J; F8 t) ], V' j
  5.   </template>1 m6 u, q! c; W1 }& M+ a
  6. </ul>
复制代码
v-for 迭代对象
v-for 可以通过一个对象的属性来迭代数据:
v-for
4 Y( D' I( o, y2 [2 v9 L) c+ L, p5 f
  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;">
    3 r& q- F8 U. q4 \1 ?( ~0 j
  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 q+ Q9 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;">
    + M- |' c0 m. P9 P4 E/ x
  4.     {{ value }}
    3 V" B7 u  m, x3 G9 W
  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;">
    - C: n- [2 |9 M5 O  v+ J, |
  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;">  n; C+ c4 S' T. `* d6 L  s$ 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;">
    # d! x8 R- T- b5 e

  8. 4 p* P# ?0 p1 r+ ~4 E" h1 a8 w
  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;">/ @' o: X! N) [$ k
  10. new Vue({
    + f# I2 t0 U' I" w
  11.   el: '#app',
    8 @8 ]0 ^& R/ H; ~4 h1 k2 w
  12.   data: {, w. }% C/ y8 g0 {$ ]2 f
  13.     object: {( E4 j7 ?5 t- b. k9 g' a# u4 U
  14.       name: '菜鸟教程',, ~9 H. c" b$ }/ b+ D, u
  15.       url: 'http://www.runoob.com',5 j7 ^9 y% S+ P9 }
  16.       slogan: '学的不仅是技术,更是梦想!'
    0 b) z# V. q& ]- G+ i) Z
  17.     }1 U! b! k/ E2 y0 t1 e6 }
  18.   }
    ! R5 f6 z  Y8 W6 k
  19. })
    + B3 L1 Z# [1 i2 J
  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
7 B  L1 f, Z9 i( I  A2 r9 A
- \# u2 i8 ~$ B
  1. <div id="app">
    0 S& Z* U$ K5 F' w' d
  2.   <ul>
    , N2 c5 _7 }$ ~# W
  3.     <li v-for="(value, key) in object">  v; }( f9 r, |$ n; P) c
  4.     {{ key }} : {{ value }}! M+ Z! F, V" I3 v
  5.     </li>
    % l' @4 y; |% {- R! I
  6.   </ul>. M8 V3 E6 [/ A* r5 A5 v# k" X
  7. </div>
复制代码
第三个参数为索引:
v-for% X% b0 L3 u8 X

0 ?, o* ]6 v2 j$ K
  1. <div id="app">( S+ L0 H) N  J1 ~) R, O9 g. B
  2.   <ul>5 K" ?3 ]; `$ M! n
  3.     <li v-for="(value, key, index) in object">
    % J6 Y( `- v2 P4 b
  4.      {{ index }}. {{ key }} : {{ value }}
    - o5 O8 n2 q% `& S7 t
  5.     </li>* `& J) k7 v! x3 t8 M# u! r
  6.   </ul>
    - Z# Q0 S, u  ^  K  B
  7. </div>
复制代码
v-for 迭代整数
v-for 也可以循环整数
v-for
/ ~) v1 f) ^4 r/ t( P7 J9 m  k% M% W7 Y
  1. <div id="app">
    / {( _4 T% x/ U' g0 M  V
  2.   <ul>" L; [3 V: k& x9 y& T
  3.     <li v-for="n in 10">
    6 o/ N7 J8 s4 T$ J, p3 A
  4.      {{ n }}% z3 U7 s" ]& W  C5 ~0 p
  5.     </li>2 n5 e4 ?4 k; ?* O" H7 I
  6.   </ul>  a8 I6 E. V0 |+ W
  7. </div>
复制代码

+ E- _( e- n1 f1 D. Q3 v+ r5 {" k+ [. S6 w
1 @% b% P" f9 ]

" ^, R- ~; B9 ]* B4 W




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