管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
循环使用 v-for 指令。 v-for 指令需要以 site in sites 形式的特殊语法, sites 是源数据数组并且 site 是数组元素迭代的别名。 v-for 可以绑定数据到数组来渲染一个列表: v-for 指令$ U5 c/ |# E$ T- f
- <div id="app">
& F; M @% a2 N8 g) T - <ol>: p3 |2 J. \1 r& u, U# V( _& k
- <li v-for="site in sites">
# _0 k; a! l5 L7 i; ]8 j- j - {{ site.name }}1 c' t. Q" `) N, Z( d/ @
- </li>; S2 y; p. C! N# z" P& \& E
- </ol>
- }, t; G. A7 L. K - </div>+ u( I6 I. P3 }! A
- 1 w/ T4 H2 O& J3 R6 B/ E
- <script>
* _ _) E0 Q- ^+ U& O% x. {$ v - new Vue({* D: c; }+ Z+ X! I2 h
- el: '#app',
5 y8 m/ Z8 Y4 z! ]2 R& C - data: {
/ L3 W, p* x& c. {( J - sites: [
/ n6 ?" V' J& Q3 W& B2 P7 s& K - { name: 'Runoob' },! X% j0 ], m6 ^# `0 ]9 a
- { name: 'Google' },
: d2 @! C) r1 W: m5 I' H - { name: 'Taobao' }
_8 S% o, P1 Q - ]+ N! m, ^% K0 y9 Y& U. q
- }0 s9 V* d0 g1 u, S* `
- })- L7 s; F4 y" M' g+ H
- </script>
复制代码 : S& e" ]/ r* J
/ x; j9 V3 ]! g% ?+ I模板中使用 v-for: v-for
" M+ K1 l$ W# n# s& R6 Y0 {8 _- <ul>
+ L4 R" N- S) b9 M! ?1 D6 L) a - <template v-for="site in sites">! `0 W: [: d+ [' p0 o
- <li>{{ site.name }}</li>3 }2 {: U+ a& d! T g6 O
- <li>--------------</li>
8 B4 I9 A+ v4 F" F1 V - </template>& t! F$ o, \! E, x D$ p+ f
- </ul>
复制代码 v-for 迭代对象v-for 可以通过一个对象的属性来迭代数据: v-for6 Y" x" c# X$ H5 o+ _
- <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 N2 B/ J/ X 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;">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;">
2 _ B1 x1 g4 ?1 y 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;">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;">
4 Q; ~8 @- |: J$ X! p - {{ value }}; E- i% [* F" o* [1 c- f
- </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& J1 t0 N# N, v6 \/ H2 c) q) j, R* 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;">! O3 |* }! ]3 S0 {: 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;">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;">: y$ c* C+ y) W" Y
- ; d1 F. _5 |2 f
- </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;">
P( o" G2 a/ t+ |4 J - new Vue({
4 x. s( v5 t& R a# H - el: '#app',
3 f9 m" Z \. W e9 D! N - data: { T7 E% _. J# B; P0 l4 v
- object: {
/ O' o( m: B7 b. a, r - name: '菜鸟教程',* m( A2 C- J3 @2 e
- url: 'http://www.runoob.com',0 o+ I# p1 }% s K
- slogan: '学的不仅是技术,更是梦想!'4 b* m3 \# d0 ^& L* M) W7 H
- }
+ `( a' B3 N& u6 ]2 Q. a - }/ O8 X" o6 N/ P% i6 W
- })! Z/ E S+ s7 N" f$ f
- </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- l3 v* |& G( U
( {" V# x% M( J* D# R+ p
- <div id="app">
3 H* p; c1 o. X* S: m( w' Z - <ul>
- T3 P! ^) _, w3 B- m, H& v T - <li v-for="(value, key) in object">
1 |! v# `7 L7 [9 }/ Q# } - {{ key }} : {{ value }}
5 B+ T( ~: X, ^ - </li>( c7 I) I! @+ R5 \
- </ul>- Q# Z5 S9 c! F
- </div>
复制代码第三个参数为索引: v-for
/ A; T+ U6 ~8 s0 p+ H5 U
* z# M- D" f9 [% x$ @4 E$ T- <div id="app">9 \" D# N8 q, e9 k) R) F
- <ul>
; X1 R' G9 e8 \* [. f# ~ - <li v-for="(value, key, index) in object">
$ G" u% i$ N' h3 a4 o* t3 Q - {{ index }}. {{ key }} : {{ value }}9 l! W j. P; o# O: u- Z8 C
- </li>
6 F4 P* A/ ^( M - </ul>+ t7 E+ C9 y: q o
- </div>
复制代码 v-for 迭代整数v-for 也可以循环整数 v-for N, E* x. I' S- {
& Q0 x6 V# a" S- Z$ N; q+ U
- <div id="app">6 e" y* h2 m5 Q- P! f
- <ul>1 H3 D5 d9 m- L# R$ R3 d5 b1 x
- <li v-for="n in 10">2 u) w/ _5 k: G* M& m/ l
- {{ n }}) H# @- U: [; a& @9 j; O/ V- A4 v
- </li>6 I9 Q; E( ]' ?! s8 ^5 Y8 V
- </ul>4 j/ Q I; {' b# I2 E3 i
- </div>
复制代码
' I& M x5 u( M; G# B
: U P( e3 e1 l4 Z B( [
) e+ X5 ^) l2 A! S: h
}& I6 {: x- ]; u: T& W+ R |
|