管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
循环使用 v-for 指令。 v-for 指令需要以 site in sites 形式的特殊语法, sites 是源数据数组并且 site 是数组元素迭代的别名。 v-for 可以绑定数据到数组来渲染一个列表: v-for 指令
6 F H# K& J: M3 K& b" Z- <div id="app">
$ K8 I7 q x; F8 L* V- J. C4 A - <ol>
6 J: @* t |, z( s4 |; M - <li v-for="site in sites">
6 J. U3 p+ z/ |" C1 ^5 o* C - {{ site.name }}$ Q. m: T- N: Z4 X4 X+ @
- </li>
5 C1 o: A, m9 B6 {3 s8 n - </ol>
- ~& l" I$ j" s: w) m - </div>/ ?8 K6 ]+ P% x8 D) B
-
' Z- A$ y3 D. ~ - <script>
1 p/ e- X Q a# Q! _+ f/ U - new Vue({
5 b7 P0 ^' m( u. H7 I, q - el: '#app',
- Y6 u: ]& u- [ - data: {& h7 o8 e" H, V
- sites: [
: a2 J( o2 w! Q3 R* A2 ^7 Y - { name: 'Runoob' },* D( H( S5 z7 H! W, x) h
- { name: 'Google' },
4 {* x0 t2 F8 l/ j; ^! [" n2 C- Z1 U - { name: 'Taobao' }9 H" k: C9 Z" g0 F8 S
- ] j. h# N! Y( O
- }, q, h* d' u S- y p7 _
- })
. t3 S8 P: G! ~3 m - </script>
复制代码
. O1 E) A4 m) J) r
3 w5 ?8 m% n5 X$ q模板中使用 v-for: v-for
+ v; i8 `( L. k% y5 X- <ul>) f9 T& F: \4 p# b1 |
- <template v-for="site in sites">$ l8 G. l6 H: y
- <li>{{ site.name }}</li>' A* s6 D- y& | R& B
- <li>--------------</li># @* S- N( Y( E% ~4 Q
- </template>; k; M4 L: H' H
- </ul>
复制代码 v-for 迭代对象v-for 可以通过一个对象的属性来迭代数据: v-for
& _) L( d* C4 @- <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;">$ ~5 k1 J3 N" v1 j% [' K5 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;">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 g/ ^, q6 ?' n, 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;">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;">
- H2 ~# u* _2 r - {{ value }}$ [+ f4 a K2 _) [& 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;">
) {. }0 O0 Z- `' [0 k. 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;">; }! e# {: p4 p9 [
- </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;">+ O- V# p, U4 \/ W6 I7 `) B" E1 ~, K
- 8 g. {& l# j: U1 n$ [ 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;">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;">
& z0 E3 s3 x- K - new Vue({* m" a1 L9 H0 @3 M8 F2 F8 E
- el: '#app',, I* G. c, B0 q, B V
- data: {
5 U% _6 ~( e& m$ q1 Z - object: {
3 ^$ v9 x2 b9 W: R - name: '菜鸟教程',0 T, D; \* E$ t+ i; J; P: ?
- url: 'http://www.runoob.com',- D" Y, w8 P% Q
- slogan: '学的不仅是技术,更是梦想!'5 s" B, ~; f2 r! a" E
- }# _1 h* M/ g# Q3 j: P
- }
1 T/ M* t+ ]1 W/ t( _0 q - })
3 O8 O( E Q: A1 }: \8 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;">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
8 P w+ Z% @7 k; ]0 Q
2 ], F* R1 r, B+ E- <div id="app">
# k) C+ R- J& Y) F& P - <ul>
5 ?! h! G' _9 }( ^' i# u, z: y - <li v-for="(value, key) in object">
% C M1 a8 c0 W5 @ - {{ key }} : {{ value }}
; ]( h, @8 E; i3 | - </li>
2 z' v/ [# }/ ~7 p9 V! n% D1 d - </ul>) D$ K7 m+ u( w, p: a' A
- </div>
复制代码第三个参数为索引: v-for; T# x8 u) p( W
# B( L$ C* ^, k- c9 d' j
- <div id="app">
3 Y( s7 [# ~, j1 \; @. O8 u - <ul>
. |6 X, ?% _9 r - <li v-for="(value, key, index) in object">
" Z, ~7 K( A4 u$ x# c6 V - {{ index }}. {{ key }} : {{ value }}- H9 p! Y6 _- m8 A4 n
- </li>0 S* y. e# m _/ T
- </ul>: {' K2 `! c2 g( h" w
- </div>
复制代码 v-for 迭代整数v-for 也可以循环整数 v-for
, s; Q2 z$ w, T! G
+ C% z- W4 p0 y$ h. s. T- <div id="app">- I2 Q; M# u9 a
- <ul>
3 m$ B$ D- a; h - <li v-for="n in 10">
0 p5 F8 s/ \' w9 o5 [4 w - {{ n }}
8 i H6 r# i9 O4 X6 ^6 Y E - </li>
1 t" \1 N& D; K - </ul>2 L7 v$ K3 `% p3 f% \$ z
- </div>
复制代码
1 l+ x. Z7 q V5 \8 @( D
7 O1 Y* C1 r d" _4 D5 }6 W2 Z: Q h: a
9 y% b9 T/ }% C: Z9 q) H" T2 \& t4 s& m- w6 b( u$ H- w
|
|