管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
循环使用 v-for 指令。 v-for 指令需要以 site in sites 形式的特殊语法, sites 是源数据数组并且 site 是数组元素迭代的别名。 v-for 可以绑定数据到数组来渲染一个列表: v-for 指令
5 T4 z8 o0 U0 B/ Y- <div id="app">5 d0 g: C% ]* r C- ?& G% l4 P% S
- <ol> V5 o: a; @" z E+ l( H
- <li v-for="site in sites">
9 b' T3 d; v8 B0 H# X% N! Y" k - {{ site.name }}
( ~4 I! ?- q6 T0 [1 U+ b - </li>
& F( R M7 Q# Q - </ol>2 L; I. ^ z) W2 ]+ G6 o1 k+ R
- </div>. n" X" Z/ d4 X; u& h: C
-
% m( l$ r& Q9 t- X9 ? ~1 y - <script>
+ W! ^0 U2 c1 G' i - new Vue({) o, f+ o u! \4 _0 s0 c3 H
- el: '#app',
$ i3 I. z" [: Y4 e) p0 M7 D' o+ V - data: {
1 @/ S5 B7 h. i/ j- S - sites: [
( }7 b; V3 H% r* D/ |4 t3 j- q - { name: 'Runoob' },7 G# E: Y2 x3 u- E# m3 Z
- { name: 'Google' },% W) _( r! F* W& e
- { name: 'Taobao' }" y U" N: f* a' { V' R* Y, Y
- ]
" U9 a. P' K( Y# G2 z! i7 V - }1 J0 N/ C# T! X0 q5 z; [2 W
- })( @7 v8 f4 w0 `- K! t
- </script>
复制代码 / X# {: O, |2 h" [3 r2 \
0 ]# l- y' s- t6 s# s8 y8 X, ?7 [( r模板中使用 v-for: v-for
8 O' S" w/ y8 o( L" K- <ul>
3 }) ~0 l& t( `" O - <template v-for="site in sites">
6 D5 q$ ?7 w1 h _* }9 s+ s - <li>{{ site.name }}</li>% x5 V" z% k; a
- <li>--------------</li>
3 R! T6 C$ l, N D( ^3 c+ @, a1 j6 } - </template>9 d- s4 l7 X8 a4 b
- </ul>
复制代码 v-for 迭代对象v-for 可以通过一个对象的属性来迭代数据: v-for
9 r0 |7 G; W( h' L, K2 V# W T. [& B- <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;">% M" w; R( P* D2 t4 N5 \! 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;">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 Q$ w! j3 r6 ~" 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-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 g9 U+ E/ ^- n) U8 W# z - {{ value }}" w6 A, C, _" J, p/ X4 [5 w
- </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;"># n) {7 h. r' S n( {. d& L
- </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;">
7 C R* h6 L' 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;">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;">
( a: W/ T8 f( H' K+ e: D - ! |* }% _/ G0 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;">+ D* C' Z: H! j" c+ L$ b. L
- new Vue({, e6 V% U1 ]6 J$ s2 ~
- el: '#app',
6 M1 V; D1 p- {3 I- r" w - data: {
, E/ v8 W1 ^ C% N - object: {( x$ j! p' I) `, z& P8 _3 ?
- name: '菜鸟教程',8 d5 n0 X- I# L8 f8 @9 D3 z
- url: 'http://www.runoob.com',% u# x c5 K% L; A8 H, x
- slogan: '学的不仅是技术,更是梦想!'
1 @$ C, R3 [! r. u9 ^ - }
4 \; }& [3 Q7 S- x8 y y - }9 m; x6 @& E i
- }). f/ g( L. r5 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;">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 |7 C3 {* V. }) {. e
' |' P. P7 f1 [; N7 i" w5 O- <div id="app">' p) t' c- s' e& q$ n! A
- <ul>
4 Q' S1 B5 Y4 P9 A) h/ y - <li v-for="(value, key) in object">; C, z9 H' t" y) {9 Y8 @
- {{ key }} : {{ value }}
1 F- O0 e. [) r, F0 r3 ~* Q! _+ o - </li>$ V! t, C# O( W
- </ul>2 b: F" E# Q9 z
- </div>
复制代码第三个参数为索引: v-for
/ y# s; H2 z( x- ~( H5 K5 ^0 W- Y7 \3 ]6 T9 z
- <div id="app">; |9 w+ C/ H8 T# t
- <ul>( ^! y( s, ?% [4 e4 D; X4 q
- <li v-for="(value, key, index) in object">3 N- M7 Z" u' ]4 R
- {{ index }}. {{ key }} : {{ value }}
2 _) f9 a! G! W) Y- z& f - </li>
0 C; O# q2 }- [! |' b - </ul>* C! K% Z C$ g5 y4 L: E
- </div>
复制代码 v-for 迭代整数v-for 也可以循环整数 v-for
1 ^/ t N5 b/ {3 W7 u5 J6 U. k. l8 F, F
- <div id="app">* @0 Q; ^" U1 L) V
- <ul>
6 N" S' s f7 } - <li v-for="n in 10">
+ X4 ^7 i" \+ [* }, X% }; ], y - {{ n }}
1 R( D0 S2 F S6 Y6 U& } - </li>9 ^) T) v' Y" w
- </ul>" N9 I+ D! C r2 n- Q
- </div>
复制代码 , L. ~4 ]4 P2 J
8 T$ V& n2 E& S
) l9 v0 I+ `/ o
1 P7 _6 Q& p- ~# S5 f4 y |
|