管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
循环使用 v-for 指令。 v-for 指令需要以 site in sites 形式的特殊语法, sites 是源数据数组并且 site 是数组元素迭代的别名。 v-for 可以绑定数据到数组来渲染一个列表: v-for 指令
2 \8 J7 r% x& k3 t: i3 l- <div id="app">2 ~% c/ G% x' L
- <ol>
% U/ J) G1 Y. m/ k0 M3 y8 L - <li v-for="site in sites">& x3 T/ @" O2 L1 h4 B( t& h) u
- {{ site.name }}. l8 q# ^+ H1 O" w. h
- </li>8 p4 H0 n7 S7 q `
- </ol>
; @2 z i4 o0 V' c# t - </div>( w+ l Z' B' t8 u+ Z8 I, {
-
" v8 o6 T. t( n M/ d/ F) u - <script>
! D2 B% C: p$ f9 Y - new Vue({. M! B" N$ w6 l
- el: '#app'," {9 _! t. Z, f% q
- data: {+ N m% c) o& n
- sites: [) Y4 z1 m3 \3 }5 B* I) B. P
- { name: 'Runoob' },+ P5 P5 d+ A w& g. k* s
- { name: 'Google' },
5 V. W8 ]# D; x - { name: 'Taobao' }* l5 O, [5 E" U) F
- ]
* X2 e0 S7 r5 Z" z$ i - }4 R. N5 c a# U t9 ~
- })
; ~) N5 _8 N+ C6 [ - </script>
复制代码
& ?, i( q: w/ ]; x7 g
- v: X5 I, |) z* G* y' o模板中使用 v-for: v-for
" f3 H, r, K: d5 x: f- <ul>" L; }- @1 x% [
- <template v-for="site in sites">
+ w! J5 e5 K3 f% r - <li>{{ site.name }}</li>
! b2 j3 c F% |/ F3 r: p - <li>--------------</li>
' v1 s* q7 ^; d+ a$ U - </template>
! A' D7 l- e) E* f& R - </ul>
复制代码 v-for 迭代对象v-for 可以通过一个对象的属性来迭代数据: v-for1 V+ A) `, b; y. H+ ?3 c
- <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;">
( ^) }: y! ^) c8 Y - </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;">
) q. F& a: Q( Z6 L* |% V8 _ - </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;">
0 i! \* e- h l" ? - {{ value }}
, V) A: K# k/ h, h0 [ - </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;">
2 p* P6 I0 o( d/ q; z# o s2 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;">% [/ L6 t0 c1 ]4 b# d, n: 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;">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;">) E3 R1 X4 M. i1 Q4 R; O2 m
-
" }: k/ |) b4 X( 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;">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;">3 d" f# l: w$ T6 T# x
- new Vue({% m7 d3 N* O2 @8 T) b0 }
- el: '#app',/ K1 h3 k [- N4 e8 M
- data: {
3 D! k$ w( |+ X - object: {4 S& J+ X( O. ^ r
- name: '菜鸟教程',
/ C; T; X! @/ X$ C8 R# x+ f - url: 'http://www.runoob.com',0 A7 k$ Y6 _. r' j; O( d
- slogan: '学的不仅是技术,更是梦想!'
( Y' I6 G4 Z2 W# R3 s - }- \) ?2 z }1 Q9 Z$ I8 D. z
- }' q. ?9 e+ }6 m8 e9 k1 G5 g
- })9 b+ S' D1 i, U/ @: 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;">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
( G. [8 f# O1 v) q8 l5 u
( X+ I# W5 L% \- <div id="app">
5 v7 V! v+ r. w) Y0 s - <ul>+ R3 L" g0 M3 A" V- z4 f4 G$ b
- <li v-for="(value, key) in object">3 `0 A, r' L" c/ v9 {
- {{ key }} : {{ value }}7 i" a) }" m- F3 q5 s
- </li>/ F i/ Q8 |4 y7 i, K9 H. X, N
- </ul>! ~$ U. y! i8 G2 a" v
- </div>
复制代码第三个参数为索引: v-for
, S2 ^+ C6 h' m6 Q1 I. Z
8 c# ^' f/ \3 R: b8 M- <div id="app">0 v. b7 A$ f4 {! e3 O
- <ul>' A6 v$ [) a! l: N# L! ^
- <li v-for="(value, key, index) in object">9 p' Z0 R1 t+ D0 X
- {{ index }}. {{ key }} : {{ value }}+ s) n" x9 `# K- f" |( g+ @
- </li>
" n* e0 K1 a7 \ U& e, Q R - </ul>
3 ~. L( U! a |. E; c - </div>
复制代码 v-for 迭代整数v-for 也可以循环整数 v-for N* j, d u5 d" P' B) I
7 j: h7 s6 r, B( u6 s O) Y
- <div id="app">
4 |& I A% }# g* X F; F - <ul>; S8 Z% c' ]2 U& p |! H1 e
- <li v-for="n in 10">
5 b5 D0 d! r2 V" i - {{ n }}
3 K( u0 i" }9 R; Z- O8 N5 X - </li>7 k0 a3 U7 A) v! n
- </ul>- w1 G2 ?" l, `( w* B
- </div>
复制代码 . Q4 S8 p" N9 b4 f1 ^' J1 K
/ C) n& C7 ?- S# @! p) A' S( F: x. g1 e$ o/ I) ?: v* j
- y6 q* \: e& j S) w" _$ e7 u
|
|