管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
循环使用 v-for 指令。 v-for 指令需要以 site in sites 形式的特殊语法, sites 是源数据数组并且 site 是数组元素迭代的别名。 v-for 可以绑定数据到数组来渲染一个列表: v-for 指令
& A/ B7 A0 ?8 F- <div id="app">3 n' \% h5 J' {# a
- <ol>
4 A! h; @8 H0 K - <li v-for="site in sites">( e: G( w# L/ O
- {{ site.name }}& p7 @& i# A6 S0 X) ^4 D
- </li>
: [" N2 C0 k) ] - </ol>0 V" o: P4 U, |* }7 u- u; J' {# N" o
- </div>
- K9 ~ ]! m. ~3 U1 z' H$ O4 [ - , i6 K( j7 H: A* g$ T+ W$ y
- <script>
; w' n0 p7 j$ e - new Vue({
( e, K, w- y% s( ` - el: '#app',7 H1 u+ n# d" H! a I7 V
- data: {
7 i9 l, o. a2 ~ - sites: [
' o. I- G& l* ?6 { - { name: 'Runoob' },3 z$ ^4 {" f% C% m' b: r$ k/ B
- { name: 'Google' },
+ s' d, b# q# K7 K$ R: v2 m - { name: 'Taobao' }) G- S( x! X, d% C
- ]
9 B. n/ f- P. H' |2 D ^' z+ O" g' S - }2 I, {9 } N* P
- })
R5 k& L. u3 l9 k - </script>
复制代码
: O) @2 y( U M/ t3 c t2 _
+ x, L: g- [# t! f( K( U# I模板中使用 v-for: v-for
( L K0 h* L5 R4 w, L0 [# M- <ul>; R/ D* f, j* G0 Y& p4 ]
- <template v-for="site in sites">
" Q# ^0 |* H; ?6 O2 S - <li>{{ site.name }}</li>8 ~0 |9 _4 s$ Q6 ?: ` G; s# s
- <li>--------------</li>8 ]" g- h: B4 b/ I# {1 R' t9 C, W
- </template>
! ?5 _9 p2 f% O) U( l( C! m! o5 \ - </ul>
复制代码 v-for 迭代对象v-for 可以通过一个对象的属性来迭代数据: v-for0 I* a9 E6 n! M% d& q4 U
- <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;">
% w G d# I# }6 W5 S) o; m" ? - </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;">
; A! d& m+ d6 a" Y' ]6 q - </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;">+ m8 J2 K, H0 v5 F/ K
- {{ value }}2 S9 \0 L( E4 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;">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;">
" O6 d" Y3 {1 k W4 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;">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;">
0 ^6 Q) _" G3 R) O- Q$ T; ~) o" 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;">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;">
2 ?9 l; Z- c) q4 R; Z) R -
G$ P; u- x* ^5 l& J0 \ - </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;"># t8 G+ A d3 J i0 W
- new Vue({' M; h7 K' k. v i1 B
- el: '#app',
, h" P$ _& m9 v, B' z - data: {
* N/ H; w9 v8 e0 ~. l - object: {
6 S! A0 t; S2 n) e& E - name: '菜鸟教程',+ B' ]& y7 w8 Q7 p2 s( G. I
- url: 'http://www.runoob.com',
! l7 a& _( C6 [ - slogan: '学的不仅是技术,更是梦想!'3 L5 l' F" u5 p6 y
- }- X. ]7 @' ]" p$ |3 `: S
- }, U/ P2 j4 S+ A
- })# N1 @8 s* I2 P. ?$ d! R
- </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-for3 @+ [% K4 f- K+ ]8 `2 r& b0 o
5 }) z3 Z% Q% P* E
- <div id="app">: q! j) ?# L- a6 L
- <ul>$ V" _8 C; c$ R2 g" i
- <li v-for="(value, key) in object">
! Q1 C6 V3 l6 b; v& E - {{ key }} : {{ value }}( S# S& X8 ~' n8 E
- </li>) y. N0 ? Q$ A2 Q
- </ul>' T) x7 P4 h$ u5 ~( ]0 V8 B8 q
- </div>
复制代码第三个参数为索引: v-for! k t' f2 `* s) l( Z
6 g% t5 c. s4 s% M. s" V( R# e- <div id="app">
) n, `. o5 U0 K6 D! `- S - <ul>
! {. {& e" v3 F6 o1 a V - <li v-for="(value, key, index) in object">
3 Y( J0 D1 Z/ B4 a - {{ index }}. {{ key }} : {{ value }}: l- f, x" P" L/ A. t
- </li>7 {- f* J% I( X1 B; ~4 ? Z
- </ul>; ~' [% S2 t" f6 ?! K
- </div>
复制代码 v-for 迭代整数v-for 也可以循环整数 v-for
. ]. S+ g0 G q: z3 c
, P# e+ C* Q ?1 i d' y- <div id="app">, L" L) }4 {+ g/ h
- <ul>0 W5 A0 l G# l5 M6 F, b5 M
- <li v-for="n in 10">9 p4 ~$ q1 x3 T* r% B
- {{ n }}
3 _2 i1 q! w# F/ Y" P - </li>
5 k) E$ u% ^6 U" F% c4 [9 c - </ul>0 y* H' z8 {5 p- I
- </div>
复制代码
a# _9 U0 U7 M5 k. z4 r7 N O1 Z1 f4 s/ L
2 x& v& K1 o" b) @% M" x# E3 f0 ]6 I {7 c( `. I8 ?1 [
|
|