管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
循环使用 v-for 指令。 v-for 指令需要以 site in sites 形式的特殊语法, sites 是源数据数组并且 site 是数组元素迭代的别名。 v-for 可以绑定数据到数组来渲染一个列表: v-for 指令
6 O' K ?: |" z0 i2 V4 y$ p# e$ N( G- <div id="app">7 W4 x6 e9 |. d7 X/ z7 J
- <ol>. m% j% x" T5 T3 s8 s/ H+ R4 Z- b
- <li v-for="site in sites">4 P/ V/ G5 v- y. a1 v# P
- {{ site.name }}
0 q A+ {+ q: {( L2 U8 D( U$ @ - </li>
6 O: ~+ `$ X+ T5 Q; a* ^; g+ N0 D - </ol>
4 _1 f) x: c' H" y; R; o- I - </div>: b; T( N1 |3 ]3 H' B1 d) I
-
- t2 R, |' v8 k! ]& @ - <script>
) M: }( h F' |3 S w1 |1 j, | - new Vue({; k, Y. @* M7 n" p, K. S+ F
- el: '#app',
% Q3 `6 p0 j6 s5 L9 ? b3 U - data: {
7 ]/ Y; G8 \# ?" f8 x& H/ t - sites: [
4 I& Z6 C# ~% w1 U/ @6 E: \ - { name: 'Runoob' },
4 n8 [( _ N( F. K) @ - { name: 'Google' },
6 A! s- Y. U# I3 C" v, k3 H - { name: 'Taobao' }3 c' \8 S( f# W( m: h) m7 M" X
- ]
0 l: B: v& T5 Z - }# d$ ?/ {* t& I2 F! @7 w! R! f
- })4 ~5 F6 ^% H0 L4 i
- </script>
复制代码 2 m0 W' M* j% R' G0 W
# d- l U; t5 e4 R模板中使用 v-for: v-for# T1 z3 g/ I6 U3 V5 a* k4 }. h t+ c
- <ul>
& P$ z" e t* i( L! Q, C6 U - <template v-for="site in sites">
& m1 u9 J8 ^" P+ w9 N - <li>{{ site.name }}</li>
% `6 b" F3 |& c4 L - <li>--------------</li>
2 w; G- [4 W5 X4 l- Z/ P6 A - </template>5 T- s6 [" K, Z6 C r" z. f9 \+ ]
- </ul>
复制代码 v-for 迭代对象v-for 可以通过一个对象的属性来迭代数据: v-for
/ n$ d5 m1 t7 Y- <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;">( {4 ?# V9 M; a* c+ z$ ?4 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;">/ _6 B2 S( @0 J6 j2 r0 k4 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;">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;">
( U! g& {- l. i& c- \% A - {{ value }}
) K; \& Z7 c# D3 j1 ^! 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;">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;">
- \) k. a; g8 {! S+ q" q* U' H' T0 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;"> c$ ^8 E. ~* P+ ]6 A; N
- </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;">
. S& R( Y+ P2 l# |0 ` -
+ c3 B* _8 o+ 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;">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;">
- }3 W6 b4 X8 B - new Vue({9 X$ R) g9 e+ t( e) e z& i
- el: '#app',
: f) V+ _7 K% D ?0 F: E - data: {
. _6 S5 ^# h, o8 i9 j5 f. z9 L+ v - object: {
3 f' z1 s: u3 a9 @% u* Z# I - name: '菜鸟教程',
* F3 a* g! |0 y0 }: G* C - url: 'http://www.runoob.com',
( E+ T; H1 T" E( i/ r, W - slogan: '学的不仅是技术,更是梦想!'
5 d) ? p- ]; K& j7 P) U - }
5 X7 E* ~* m* A+ v7 S# l - }; ^ Q+ ]- A2 g+ Q
- })+ Z0 U/ Z+ u$ D- 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;">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
) d. x+ X' K. Z4 Y/ j7 }7 S
# T# {1 Y1 k+ R2 \5 p S" f1 e- <div id="app">! I5 K, A0 G' K+ E
- <ul>7 {3 M! V! @6 j a4 p# U% V
- <li v-for="(value, key) in object">- \5 j' }4 R4 |# U( Z1 e
- {{ key }} : {{ value }}) I. a8 U' ?6 {* q
- </li>) Z7 K& ?, D% {
- </ul>' o; I+ g/ l" w" j6 R
- </div>
复制代码第三个参数为索引: v-for
' B9 s3 G1 ?$ P4 L _* A9 M! r1 G3 U
; i& L9 [; j6 N% q! d* Y- <div id="app">! z& g* r) {8 N& J
- <ul>
4 {7 [9 ^8 O! h( B3 M1 w6 w" L - <li v-for="(value, key, index) in object">
" W& t- ^+ I: ^! a. k/ @ - {{ index }}. {{ key }} : {{ value }} Y% Q( j( e* H5 p/ M: x5 G
- </li>
2 b& T1 m$ q" X& k% t - </ul>
4 j, U% U* a" ?3 c - </div>
复制代码 v-for 迭代整数v-for 也可以循环整数 v-for% ^" m7 N9 }: @* w+ E9 j
; a j0 x4 l5 V% \6 e
- <div id="app">
+ E2 p: ? Z1 U: t+ C0 [1 T - <ul>
8 H- t, E9 I$ j/ b. F2 E" W0 }" s - <li v-for="n in 10">
) G) \- V$ Y! N - {{ n }}6 }) q; y+ x+ s$ f
- </li>
! j: Y0 @" S7 d: V - </ul>- a6 b8 y. P3 ?
- </div>
复制代码 ; m; G$ f1 }7 l
, D4 G. n" D: V. \, {- ^" p
) g) s0 ]; A: m) @ W9 N$ f) }: Y6 _, z& g3 N* E
|
|