管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
循环使用 v-for 指令。 v-for 指令需要以 site in sites 形式的特殊语法, sites 是源数据数组并且 site 是数组元素迭代的别名。 v-for 可以绑定数据到数组来渲染一个列表: v-for 指令
9 Q2 y3 {/ r" ~- <div id="app"> G- G2 d# }5 v) \2 k
- <ol>
/ n! G% L* K2 s4 K& W& I) P& Y - <li v-for="site in sites">
3 ]1 g7 J- w1 W - {{ site.name }}/ v6 p9 P3 v, i; I2 u i, f" O' v; {
- </li>/ X! a" }' a) c7 w8 x
- </ol>
0 K4 O% K* K, ]7 P% G - </div>
! z6 v F0 p5 W2 x7 C4 ~ -
7 |# X) q7 j {3 @) Z - <script>
! G3 Y0 ^6 w% h( }- f - new Vue({
6 R2 M9 r% Q' Z* K - el: '#app',
: [2 l2 S7 u& U1 F1 j - data: {2 ~' n& P; J! J% H
- sites: [6 o. l. `& p: n, j
- { name: 'Runoob' },
% q2 y' A& Y. }; V; O! F - { name: 'Google' },
D) a& i- k, \2 i9 r - { name: 'Taobao' }. H# }" e& [$ O8 Y6 r
- ]6 \( K- u% G3 @2 j0 b3 D8 W; f
- }
3 \: |7 c9 y9 j# Q9 Y; {: V/ P - })
8 ~/ G% w* F6 } - </script>
复制代码 9 m) z8 k' ]) m1 Z
) w% \3 G* Y5 o
模板中使用 v-for: v-for
" }9 V% x3 O# q0 v- <ul>
$ b. }8 D9 N- F( s: W6 X - <template v-for="site in sites">
( ~! S0 u) T* C! f- u - <li>{{ site.name }}</li>" G4 S8 R* {% M) I9 ]6 `
- <li>--------------</li>
7 L) Z9 `; {& k8 M& N - </template>
( y. m T/ i. X& X$ ?& o - </ul>
复制代码 v-for 迭代对象v-for 可以通过一个对象的属性来迭代数据: v-for+ c9 H! ]: e/ L6 W* t
- <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;"># U! O% u+ `* N+ R- {' G
- </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;">! ?& W3 M$ W3 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;">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;">) a' ` a2 I( T/ G1 ]
- {{ value }}: r* \+ @# w- d
- </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;">5 S. ]8 {9 { g9 g/ g B1 {4 E. 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;">
( g& ~# W1 T3 d# q# 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;">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;">
, T: n- X1 f& Z! ^0 a: ?2 Z -
, m4 F3 A5 D( Q1 V( A2 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><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;">' B5 ?: }: H' x; g+ d% X
- new Vue({& T# j9 O% A" g/ ?3 v* }
- el: '#app',4 \ W5 O' ^# Z: Q4 I# D
- data: {* L$ h" b5 p9 B7 ?. g
- object: {
2 ~2 O& y) }# z& B2 E2 I - name: '菜鸟教程',
4 I) D0 L0 X8 Z7 t% Q - url: 'http://www.runoob.com',
3 n; R7 D$ \" f# k3 D# u - slogan: '学的不仅是技术,更是梦想!'
- K+ J, W p/ i% t# u1 y% } - }
: _9 f. h7 z" T - }
! p: [6 @. p* @' u - })0 f; G; P8 I; k, X0 T- S1 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></div><div></div>
复制代码你也可以提供第二个的参数为键名: v-for* x3 [9 h) E: m) o
9 d! q2 m8 f$ o; }, X- <div id="app">
7 I: T' Q2 r) {3 { - <ul>
/ D6 \: }* J. O% [ - <li v-for="(value, key) in object">
1 f, ?, P/ W5 g# T+ v; T - {{ key }} : {{ value }}
5 r, i- z8 b% E1 s5 ?4 @ - </li> a# }- E' m6 Q' C0 n3 I8 q
- </ul>, X( r4 v+ @: Y5 \9 q' P
- </div>
复制代码第三个参数为索引: v-for o) [$ @+ F9 t5 j' T ?& }
' ~& ~) @. L% m$ J- <div id="app">6 ]- E2 K& {* @" c5 m$ L. v
- <ul>
3 e/ r3 ~1 A5 ]' y4 g K - <li v-for="(value, key, index) in object">
, ]1 D2 N# V& _% o# k L - {{ index }}. {{ key }} : {{ value }}
& [9 H6 [9 }/ r) `# Z: d+ W; i( L - </li>1 l- O7 R$ E. D$ q# ^
- </ul>
) h. f0 @6 Z# a5 S - </div>
复制代码 v-for 迭代整数v-for 也可以循环整数 v-for
4 H6 S& N/ ]* E+ @6 e A
7 P% m. n5 C; l. } k# U- <div id="app">' S( @7 v) d& Q
- <ul>' D( @ D! @: N! V* S
- <li v-for="n in 10">
4 L/ o& q) l o- L - {{ n }}
$ ]; ^4 T, R) n* Q4 o8 l - </li>
9 l# u9 s2 v3 q2 J6 o - </ul>
7 q2 k5 k2 }" [1 H - </div>
复制代码 4 Y% g( J5 e: Y
) S! Q$ C$ {& c: o; q; v3 K" ]9 _4 H; n) @2 B3 k2 M @
' @, `6 l8 Y J8 A$ K- z% Y- Q O
|
|