管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
循环使用 v-for 指令。 v-for 指令需要以 site in sites 形式的特殊语法, sites 是源数据数组并且 site 是数组元素迭代的别名。 v-for 可以绑定数据到数组来渲染一个列表: v-for 指令
9 o0 I7 }7 ?) [- <div id="app">
( r3 F3 I# l2 ?/ A' ^. B - <ol>6 p& z3 N! D) {0 ~$ D
- <li v-for="site in sites">8 @" D, j1 H; R' l
- {{ site.name }}! n6 P. v2 A% f) X5 a
- </li>* p% y% ^$ p w9 O/ [
- </ol>
1 z! e; J8 b4 M+ B" {- G7 N% N - </div>
$ |5 K% ^& Q# O! ?. _2 P -
8 X, _1 {9 @1 t. e0 n! s/ Z - <script> P9 o) B; S5 q8 ?
- new Vue({9 s: U# \/ O' u
- el: '#app',
( U: H$ P, L2 O$ s' R - data: {8 o/ u" U7 w/ @1 y
- sites: [
. q8 {; t& C% U# X8 `( P3 Y, q - { name: 'Runoob' },- H* ?+ k* M5 Q* C5 e/ g2 I+ M1 n
- { name: 'Google' },2 G( S0 K& m1 Z# i
- { name: 'Taobao' }7 y+ W" | n6 x9 o
- ]3 u0 d8 @- T/ S1 ~2 f& R
- }; O9 I4 j/ M- @* \
- }), g1 D6 g7 P# y+ o! ]
- </script>
复制代码
x& d8 `2 r: E( f; G% W; ~! p. j& a* p7 r$ g: e
模板中使用 v-for: v-for) s+ ?+ ]3 S( c: o' `
- <ul>
: a L# b2 c( Z# l/ q% K) b0 @1 m - <template v-for="site in sites">
! p# X. y) \! Q0 \# h5 k2 p' | - <li>{{ site.name }}</li>
1 J7 I6 Z1 x. U ]) @8 @9 L+ w - <li>--------------</li>/ [& i5 ?. w- j% y9 J; i
- </template>9 Z0 _. d C: Q) ^8 F
- </ul>
复制代码 v-for 迭代对象v-for 可以通过一个对象的属性来迭代数据: v-for
; h; | O. P0 |4 x! M+ F- <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;">
% |0 ~1 R0 y1 |6 [ - </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;">* ?( @" ]9 k- h! Z
- </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;">1 ?- }6 i( r( Q4 G
- {{ value }}
' {+ f; d+ Y' q% P - </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;">
: E* F, y& E( r* t6 E - </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;">8 M! L C+ ~2 }0 W% 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;">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;">
3 `3 d! c Q8 U0 E -
. R6 o, Q7 b8 D/ n% u - </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;">
; l9 Z7 Y9 p$ M5 K" \) f; C3 b% S* j - new Vue({
; U0 a/ W5 _9 Z: v% Q. I7 { - el: '#app',
1 \' M) V9 Z% g7 b/ J; v2 w6 P - data: {5 d8 s- _2 a& o0 q0 P
- object: {; I7 G4 G$ Z4 L5 y$ p2 ?
- name: '菜鸟教程',0 h0 V }. L) v2 ^' ^& k9 n
- url: 'http://www.runoob.com',2 j3 Y# I/ |& l3 K
- slogan: '学的不仅是技术,更是梦想!'0 q; N/ Q. Q! ]) n3 z
- }# E5 I% C5 B; ]( w% p" x0 w# h
- }
7 F" O) R* @. k9 U( C - })0 P( F& r7 z5 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></div><div></div>
复制代码你也可以提供第二个的参数为键名: v-for
% u. ]) @! z& ]; R5 K( M0 q/ Y; f O
- <div id="app">
' M. v1 m: Q O! E7 P) U - <ul>
( J* N% R6 ~* C, y- t/ I: b - <li v-for="(value, key) in object">
3 a9 m1 N& r1 O* t6 A - {{ key }} : {{ value }}
% ?3 s; [$ c) A* g% q& J- t - </li>
- }' l% Z5 m- M" v - </ul>2 f6 S4 D5 \7 }. z
- </div>
复制代码第三个参数为索引: v-for; M# ?& d% L8 G% G @) b
6 U) C" ?/ ^% ~4 {! `, h+ x- <div id="app">
: r8 c5 A4 W/ E: l9 G% `6 p/ W - <ul>
% I+ ^9 O# Z- J. i/ V5 h - <li v-for="(value, key, index) in object">. x3 B2 O7 Z+ ^" s
- {{ index }}. {{ key }} : {{ value }}
0 P9 Q8 ^# h8 L6 W7 S - </li>2 _" _3 F, }/ u+ z. b$ }. u
- </ul>- f) u; J( s! f3 \. {+ w
- </div>
复制代码 v-for 迭代整数v-for 也可以循环整数 v-for: e$ w* n% v+ I: C' r
3 ^4 C- M3 P1 G$ l: U- <div id="app">, _ T! H8 e' J& w3 B$ q
- <ul>
2 v c1 }2 h* F9 H; v* L - <li v-for="n in 10">' C7 w% u! @, ~
- {{ n }}
# ]: h3 F7 Y" P3 x1 y( E& { - </li>
' T' g- W3 T$ Y6 |4 _. {, ^ f - </ul>. ~& g* Y3 C& }, Q: M1 ^* t
- </div>
复制代码 " m' f" |! c7 P! z2 D' T/ [
8 N/ s$ t* m* _# u( _- R. F5 c0 N
, x; _& h0 P! Z" G+ C" D
. U. X4 j# N% G& x: O- O |
|