管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
循环使用 v-for 指令。 v-for 指令需要以 site in sites 形式的特殊语法, sites 是源数据数组并且 site 是数组元素迭代的别名。 v-for 可以绑定数据到数组来渲染一个列表: v-for 指令% H& d! ~& {* n2 }
- <div id="app">; N: o- O! W9 F$ ~
- <ol>
, u F1 K+ |9 x9 O# G' r - <li v-for="site in sites">
: L8 b$ }* A. N# T - {{ site.name }}) H) e, ?5 Q! R& y- v. l& [% V2 S+ Q
- </li>
- ~0 c* m5 Z" M* X - </ol>5 c. ?% b1 V, \7 t( a: y$ r1 D- i
- </div>
$ z& R- |8 U4 K7 X/ L4 m - 1 \+ ]1 @( p; Q5 R r; e' @
- <script>0 T# b$ L$ n3 C" v2 V4 E+ X
- new Vue({* \% G" Y* K- q2 w5 z/ _9 T) l
- el: '#app',
' c+ ]. U+ }3 F* \+ ]# d4 E - data: {" }; E7 u+ [$ O/ ?& c
- sites: [- U) [3 O" h/ a8 x4 C+ c" c
- { name: 'Runoob' },2 I' _: K0 i" I r" \" K
- { name: 'Google' },* S. Q$ X( C! H" u7 Y6 E
- { name: 'Taobao' }
4 p+ o) L& _3 P. G - ]. g' \" n" ^4 N* |* O* c# Q
- }
8 j- G3 q5 p$ M8 D: \. ~0 c - })
0 ^. E( B- J( |6 Z3 W: S4 ] - </script>
复制代码
2 l0 h& j7 m2 u3 @% z T: @
3 k9 q% U' W4 I8 G% ~模板中使用 v-for: v-for
* z4 g S4 s2 s* x% I' }- <ul>' q! e/ K, k# v$ M
- <template v-for="site in sites">8 s; A+ V% G% w1 P, t9 e
- <li>{{ site.name }}</li>6 c& Z" [ ~- S9 n
- <li>--------------</li>& B. I' Q: u* x. o( _
- </template>
! D B1 z4 Q. I* K9 j - </ul>
复制代码 v-for 迭代对象v-for 可以通过一个对象的属性来迭代数据: v-for, c, [: X/ \/ a, H. {7 P: o
- <div><span class="hl-brackets" style="border: 0px; color: olive; font-family: Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; font-size: 13.2px; white-space: pre-wrap;"><</span><span class="hl-reserved" style="border: 0px; color: green; font-family: Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", 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, "Andale Mono", "lucida console", "Courier New", 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, "Andale Mono", "lucida console", "Courier New", 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, "Andale Mono", "lucida console", "Courier New", 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, "Andale Mono", "lucida console", "Courier New", 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, "Andale Mono", "lucida console", "Courier New", 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, "Andale Mono", "lucida console", "Courier New", monospace; font-size: 13.2px; white-space: pre-wrap;">"</span><span class="hl-brackets" style="border: 0px; color: olive; font-family: Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; font-size: 13.2px; white-space: pre-wrap;">></span><span class="hl-code" style="border: 0px; color: gray; font-family: Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; font-size: 13.2px; white-space: pre-wrap;">
% k0 I9 U4 y8 L( m2 S2 q - </span><span class="hl-brackets" style="border: 0px; color: olive; font-family: Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; font-size: 13.2px; white-space: pre-wrap;"><</span><span class="hl-reserved" style="border: 0px; color: green; font-family: Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", 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, "Andale Mono", "lucida console", "Courier New", monospace; font-size: 13.2px; white-space: pre-wrap;">></span><span class="hl-code" style="border: 0px; color: gray; font-family: Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; font-size: 13.2px; white-space: pre-wrap;">. k& ]- I; [; d* Q7 J6 k* g
- </span><span class="hl-brackets" style="border: 0px; color: olive; font-family: Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; font-size: 13.2px; white-space: pre-wrap;"><</span><span class="hl-reserved" style="border: 0px; color: green; font-family: Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", 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, "Andale Mono", "lucida console", "Courier New", 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, "Andale Mono", "lucida console", "Courier New", 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, "Andale Mono", "lucida console", "Courier New", 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, "Andale Mono", "lucida console", "Courier New", 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, "Andale Mono", "lucida console", "Courier New", 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, "Andale Mono", "lucida console", "Courier New", monospace; font-size: 13.2px; white-space: pre-wrap;">"</span><span class="hl-brackets" style="border: 0px; color: olive; font-family: Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; font-size: 13.2px; white-space: pre-wrap;">></span><span class="hl-code" style="border: 0px; color: gray; font-family: Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; font-size: 13.2px; white-space: pre-wrap;">
! U8 m N" ^$ i; I5 W - {{ value }}. q" k0 D; W. b1 \
- </span><span class="hl-brackets" style="border: 0px; color: olive; font-family: Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; font-size: 13.2px; white-space: pre-wrap;"></</span><span class="hl-reserved" style="border: 0px; color: green; font-family: Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", 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, "Andale Mono", "lucida console", "Courier New", monospace; font-size: 13.2px; white-space: pre-wrap;">></span><span class="hl-code" style="border: 0px; color: gray; font-family: Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; font-size: 13.2px; white-space: pre-wrap;">
! i* _3 G+ @4 I c: t- T( A - </span><span class="hl-brackets" style="border: 0px; color: olive; font-family: Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; font-size: 13.2px; white-space: pre-wrap;"></</span><span class="hl-reserved" style="border: 0px; color: green; font-family: Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", 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, "Andale Mono", "lucida console", "Courier New", monospace; font-size: 13.2px; white-space: pre-wrap;">></span><span class="hl-code" style="border: 0px; color: gray; font-family: Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; font-size: 13.2px; white-space: pre-wrap;">
7 q* Z" D6 r3 G H) ] - </span><span class="hl-brackets" style="border: 0px; color: olive; font-family: Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; font-size: 13.2px; white-space: pre-wrap;"></</span><span class="hl-reserved" style="border: 0px; color: green; font-family: Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", 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, "Andale Mono", "lucida console", "Courier New", monospace; font-size: 13.2px; white-space: pre-wrap;">></span><span class="hl-code" style="border: 0px; color: gray; font-family: Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; font-size: 13.2px; white-space: pre-wrap;">; i6 v5 K- {' b5 l- P
- # S" c; t' ?. ~
- </span><span class="hl-brackets" style="border: 0px; color: olive; font-family: Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; font-size: 13.2px; white-space: pre-wrap;"><</span><span class="hl-reserved" style="border: 0px; color: green; font-family: Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", 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, "Andale Mono", "lucida console", "Courier New", monospace; font-size: 13.2px; white-space: pre-wrap;">></span><span class="hl-code" style="border: 0px; color: gray; font-family: Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; font-size: 13.2px; white-space: pre-wrap;">% m/ I+ O8 q) L9 o' t3 R
- new Vue({
+ R, L7 u5 O' R* t# {8 @* E - el: '#app',
. f" i+ w& p8 P - data: {9 V7 d& I5 l4 ^9 X3 j
- object: { x: ~- p* t% `- R3 L0 W- w6 C
- name: '菜鸟教程'," X l: l. X# @4 P& \
- url: 'http://www.runoob.com',, N3 |" C1 ]/ U' |
- slogan: '学的不仅是技术,更是梦想!'
, p, s% d' ]/ ~& M' P - }, |( E T+ h. W, ^6 N! v0 _* _
- }
$ z" A* a+ O4 t6 i4 d. B: ? - })/ M3 s b$ E& @9 K
- </span><span class="hl-brackets" style="border: 0px; color: olive; font-family: Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", monospace; font-size: 13.2px; white-space: pre-wrap;"></</span><span class="hl-reserved" style="border: 0px; color: green; font-family: Menlo, Monaco, Consolas, "Andale Mono", "lucida console", "Courier New", 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, "Andale Mono", "lucida console", "Courier New", monospace; font-size: 13.2px; white-space: pre-wrap;">></span></div><div></div>
复制代码你也可以提供第二个的参数为键名: v-for4 }4 Z( ]0 m* y/ ~8 p9 M/ z, h
7 K$ |) j' E) }+ ^* h( g- l
- <div id="app">
- y- L: _# y+ V7 u; | - <ul>
4 _. @: r3 j# |) e& i$ N - <li v-for="(value, key) in object">. n/ _0 w$ M8 r1 V
- {{ key }} : {{ value }}
0 |# Z& W4 M" ~ - </li>1 M8 h8 v$ p- w/ B
- </ul>) E2 w' S# e( q, X2 ^$ N
- </div>
复制代码第三个参数为索引: v-for1 Q- ?1 c# R R
- l' w2 I9 t2 M( ~% m/ V# p
- <div id="app">
6 b9 t' i1 U( M% Z0 H - <ul>) P$ D x P3 z1 E. ^
- <li v-for="(value, key, index) in object">
l4 K4 z+ P( q% u8 j - {{ index }}. {{ key }} : {{ value }}
7 Z% h3 D% q4 B - </li>
* }% Q! A& J8 n - </ul> y5 l* z6 M+ a1 ^0 [! {! x+ s. b
- </div>
复制代码 v-for 迭代整数v-for 也可以循环整数 v-for4 d2 e" O2 k. {9 {: {5 I
. N; D, |9 P8 z' A0 | k* B& _
- <div id="app">
. p# e3 L( V6 s4 S7 X+ Z4 I# ]: L - <ul># L& {+ n2 x- h/ F/ [
- <li v-for="n in 10">4 |: j- _1 q0 N1 l
- {{ n }}0 e# L" b) b2 `' ]; s
- </li>
V# t; r8 e) g: Q s - </ul>6 l& J5 w- l4 @; Q, \
- </div>
复制代码 & P' N& `+ r/ O9 E
( t# b% v7 T6 v4 j
$ t- ?, \8 R$ o/ s7 V! F+ R
; z5 B+ j9 s$ y+ ^ i |
|