管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
循环使用 v-for 指令。 v-for 指令需要以 site in sites 形式的特殊语法, sites 是源数据数组并且 site 是数组元素迭代的别名。 v-for 可以绑定数据到数组来渲染一个列表: v-for 指令8 B2 c. H/ Z$ [2 h" U( L* q
- <div id="app">
& y* v& C7 U5 r, V5 y - <ol>; N* j: P6 E- H% U: i$ C: i* e4 x
- <li v-for="site in sites">+ |/ m1 b! F1 a
- {{ site.name }}. e& X- z9 n r+ ^ V1 H' x- q
- </li>. y+ ?# u! c$ ]3 i! G
- </ol>
5 u2 D' n6 N9 s6 n - </div>3 V7 Y; b2 Z+ Z3 a) d3 l
-
8 U9 R5 E+ \& c% U9 V - <script>- J8 @/ ?/ |: X. n# V
- new Vue({
7 H) I% W* O, ]% V; q. d - el: '#app',
) F: f4 B- g( ]7 s- X - data: {/ S# o% m# _1 S. v
- sites: [' p' N8 T8 O4 V: G
- { name: 'Runoob' },1 y* p8 r& x u/ X
- { name: 'Google' },. q# q4 |- q1 X7 y+ d
- { name: 'Taobao' }- @. P. v# O. y2 ]0 m+ q
- ]- K9 U+ s' t' X& b- D( Q+ O: W4 |
- }7 \6 I8 l* l* e b a6 Z9 q
- })
s% x0 g0 Y6 f - </script>
复制代码
7 N- h" I; u% j: |- Q' `/ u( i9 `6 e }: d3 l
模板中使用 v-for: v-for
$ O% a, [5 r, P9 u- <ul>
! \/ I& k* a, h- O+ D4 N - <template v-for="site in sites">5 ^/ } ]! O1 q" {0 x
- <li>{{ site.name }}</li>
/ a! l; x! n4 x# j - <li>--------------</li>7 [* y0 \! u" {3 W8 g
- </template>
2 t, u* t9 M8 }' c - </ul>
复制代码 v-for 迭代对象v-for 可以通过一个对象的属性来迭代数据: v-for
+ W& w4 d2 y6 u: n, h- <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;">
9 E! k0 \& W8 C0 `2 U9 B- X3 Z5 @ - </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;">- |2 J9 Z6 Z1 \% G& 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;">4 W$ Y" P9 j( H( A! [% J" [
- {{ value }}
9 ^2 I* `1 ^% s+ } - </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;">
( V* D# J3 T0 I4 Z$ i, } - </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;">
0 }" C4 e9 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;">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;">
! n: V2 N3 R2 q" L% S/ F - 5 z1 y$ B+ S; F
- </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;">& t; W$ K. v8 l% P4 u' P9 n7 o
- new Vue({
( t! P) E2 Y" X$ G2 b- y) B/ b - el: '#app',
2 ~6 r: ^5 {3 D, W! `( n - data: {4 h& ~) y9 y- m& r( Q
- object: {
7 F; X0 U9 x! @; X5 e - name: '菜鸟教程',
+ d8 q# ~0 Z' ^) I, ` - url: 'http://www.runoob.com',# t' N; z8 c3 B# C6 W
- slogan: '学的不仅是技术,更是梦想!'3 J1 g) z0 s' ~: ^' y4 ^
- }' ?( v& a7 q) S6 R: p1 R4 B
- }6 F; f3 E4 ~, c/ T
- })' t6 f, |7 M- r( s8 M
- </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-for
; i$ g7 @5 C7 @7 D# l! Y
& S' h. Q5 T# g8 l- <div id="app">
$ o# M/ m# u/ q( Z2 q# A( u# ^ - <ul># q3 [& |7 B* e& o8 [
- <li v-for="(value, key) in object">
7 H( d2 a# y6 ?3 I - {{ key }} : {{ value }}/ F: V5 d2 p) J' ]' s t. U' [
- </li>
% G9 D; i: b. T: e Z" N( N+ J9 { - </ul>8 I+ j' h7 g% ^, }3 W
- </div>
复制代码第三个参数为索引: v-for5 ?9 h" @, G6 [5 N7 g; s
: h% X' |8 Y6 i( {6 w$ t
- <div id="app">0 ^: o H2 D0 F1 \' C: B8 U3 N( v
- <ul>6 O3 s$ K0 U% w* M4 `( a
- <li v-for="(value, key, index) in object">
, p. z' E4 M/ z# m2 f" j% R" w - {{ index }}. {{ key }} : {{ value }}
! {5 n' U# j3 s9 [( k - </li>& o* h, |: y' @7 v1 U/ I; [
- </ul>
! R; ]6 c4 _2 o/ Z0 w - </div>
复制代码 v-for 迭代整数v-for 也可以循环整数 v-for
- p( Q) C1 j, i8 f) ?" Q. |/ }( h/ o) ?; u0 C
- <div id="app">+ b8 y4 \& r. s" k' V
- <ul>7 X( C/ R9 }) m) {8 E6 [5 V0 e& z
- <li v-for="n in 10">
1 z/ N2 ]" t* A# {# P; s# F - {{ n }}/ N9 _; a D6 O5 F& Q6 G
- </li># Y0 b6 h7 Q( o: X7 h' h5 z+ p
- </ul>
1 S& t$ C2 ?: b& G$ C( N - </div>
复制代码 ) t7 w" M- P6 J7 K- `% h
0 |' F, H3 K" m8 h/ @7 y( k& Q
1 \4 N6 f7 r' r5 `6 g y
" N* }! b" D7 j |
|