管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
循环使用 v-for 指令。 v-for 指令需要以 site in sites 形式的特殊语法, sites 是源数据数组并且 site 是数组元素迭代的别名。 v-for 可以绑定数据到数组来渲染一个列表: v-for 指令
$ e5 M( E2 f c- A- <div id="app">
% R `4 u! z. A% ` - <ol>. o* e8 \. {5 J# T6 X# \
- <li v-for="site in sites">' [! ?9 j( e0 c I6 I* T. p
- {{ site.name }}6 G' u$ i8 Z; {8 N6 O+ Z/ K5 {0 C
- </li>
' s! Q& P/ z( v. R - </ol>( @2 \$ L& p+ J" z0 ?/ J
- </div>3 {' a6 r% v9 {9 ]. a
- - ]' J8 _- F! p
- <script>
+ L) B' q H ~$ C9 N - new Vue({
% f: N1 K1 ^$ X: h4 d8 u - el: '#app',. ~' i; ^1 Z7 d* X0 m
- data: {$ G9 L" u( L# r4 p" X7 T
- sites: [
1 y4 P4 p9 B" K0 i, W! y$ O- w2 x - { name: 'Runoob' },
, ^1 }$ a3 ] a* D9 ] - { name: 'Google' },
8 ?) }; q% j0 N4 ]6 ~ - { name: 'Taobao' }
6 Q6 R9 o, p$ R7 d - ]
' F3 }& i i' q, d7 _ - }; W3 O1 ^. M+ R5 H& u
- })( V5 ~/ F( ~3 v7 A5 p9 x
- </script>
复制代码
: [8 U# k/ Y& c/ h0 T* L* R6 c' Y e: C' {1 Y8 \
模板中使用 v-for: v-for
) I6 U. t, K9 j- <ul>
) Q4 V5 J# j% d+ [4 R3 Z - <template v-for="site in sites">
) Z1 g7 O) T% \6 S, { - <li>{{ site.name }}</li>0 t% B7 k, Y4 E- T
- <li>--------------</li>
. z5 c% n! ^. A6 Y - </template># x i2 I& v. Q8 C, S- t+ h7 u
- </ul>
复制代码 v-for 迭代对象v-for 可以通过一个对象的属性来迭代数据: v-for
- a+ T: E! q% v3 S4 C- <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;">6 p/ v- L% l _" U O/ C
- </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# _5 x% I! |. 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;">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;"># j: C. F! s1 ^, Z& o f; k
- {{ value }}
' p8 z' v X9 l& x - </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;">
: z0 K9 N* y$ H/ O" ^( @& v - </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& f- r* | a' 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;">4 B9 O0 _3 }0 q' _7 i
- % S; x) d1 z3 E; V: `- 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;">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;">
1 n* B! |2 p% v8 E7 |2 j3 A - new Vue({/ p! Z& y' `5 O1 }
- el: '#app',
' @; T# ], u; J' c - data: {
8 e$ a5 |- [. { - object: {" }3 |" J/ x! x0 ~/ @. B2 B
- name: '菜鸟教程',* t7 E# X7 a- c, x& G
- url: 'http://www.runoob.com',
* K# ?9 T1 z+ z$ U6 b) A - slogan: '学的不仅是技术,更是梦想!'8 j7 a( O$ g' F) Q1 a. o$ O) @
- }
) |% ~. y) t- P" J - }
4 z/ T, R! y" m$ J4 W2 q" s9 O+ \ - })( J/ _% z# ]/ K; G0 b
- </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
+ \& G+ n2 ^9 t/ W; L) ]- `7 a6 @6 ?7 F# p' ?- {
- <div id="app">
" {' o% g! W! S! V - <ul>( W7 p+ i' i; A3 c2 `; A
- <li v-for="(value, key) in object">
) F. N2 |% `4 ], z- e* B - {{ key }} : {{ value }}. `2 l4 [& X. S% K9 o3 r
- </li>
( {2 Q- T: R0 N1 q d - </ul>
/ C) z. ]( C4 C9 u6 k) A( q& c - </div>
复制代码第三个参数为索引: v-for
# K* v1 I2 x; d+ k, b
3 I0 m) ?8 a3 T) @3 ]- x- <div id="app">( D Q* x6 ~% i2 ^1 r9 X2 y& Y$ ?
- <ul>
. v. e1 J5 z! {( K$ d/ w4 n( O - <li v-for="(value, key, index) in object">9 V% h# w, w5 V w5 f. n
- {{ index }}. {{ key }} : {{ value }}
; Y' V$ C/ j6 ~0 s- g; ^ - </li>
" ?5 R' Q& z# a2 [2 m - </ul>
7 D5 g0 C; m% q& S: N: \ - </div>
复制代码 v-for 迭代整数v-for 也可以循环整数 v-for
( S9 S; h- j* q9 l7 c9 ]
4 S. ]1 j) j0 a4 s- h: f6 T8 B- <div id="app">% w- ]' ?, S8 ~9 Z& B
- <ul>
. w- ]2 n- I* Q - <li v-for="n in 10">
" ~- x" P0 G; H; H1 q - {{ n }}+ m% t3 K+ y: Y
- </li>
% n, ^ A+ `6 r; S: q - </ul>; D/ s' e9 C- y, r; r4 K4 t/ L
- </div>
复制代码
: |5 |6 y& P& F7 i$ A) n5 Y& U' w1 ~8 U$ v2 M! Q
+ f D4 d3 H' ]- m$ i+ E
5 \0 |/ Z, s$ a" ~ |
|