管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
循环使用 v-for 指令。 v-for 指令需要以 site in sites 形式的特殊语法, sites 是源数据数组并且 site 是数组元素迭代的别名。 v-for 可以绑定数据到数组来渲染一个列表: v-for 指令
$ h- l9 c. f! Q- <div id="app">
6 H7 f3 a/ o: y9 @/ k1 _ - <ol>
. S' V! x0 U& O' K - <li v-for="site in sites">2 L$ _5 e. V' l
- {{ site.name }}
) n4 x( x0 v z, R/ m: Y& m5 o: R - </li>5 I# \: j9 E% v( E8 @4 b# B! g4 L
- </ol>% k0 V2 |/ v9 b7 R9 T. Q# q
- </div>
$ J# F4 B/ G7 Q, _ -
7 J* s4 e3 V5 N( D3 q A9 h - <script>% O- _+ V; E* A4 q! [
- new Vue({6 @: F# X) I2 R4 c3 I* @+ l
- el: '#app',
: |( c6 P5 i# k ` - data: {
8 w& W$ r/ ?6 [+ f$ ]* D - sites: [
3 N! n5 h6 M" D% \8 |7 ?5 | - { name: 'Runoob' },
# F/ Z7 K. T* r! P' M - { name: 'Google' },
; w: k5 ?7 W% k - { name: 'Taobao' }# ~ d1 E" t% W6 U) _2 ~
- ]
. [9 ~. B3 R, }) d# o% [ - }: J r. A" k1 L' P
- })
/ i+ l1 E3 E8 P - </script>
复制代码
7 N# T$ {& T& s2 e( {/ ~8 W2 p3 ]3 D& X4 n7 V
模板中使用 v-for: v-for3 U+ n" p; z/ X! a( E+ A5 w" h
- <ul>
, O+ z0 k! J8 x1 {" Y2 e! O - <template v-for="site in sites">2 R& X' U7 U g' I0 _
- <li>{{ site.name }}</li>* f$ R( T+ |2 L6 e7 Z1 s
- <li>--------------</li>
4 q- A/ U5 C* h# e+ }$ T1 W7 r - </template>7 B2 L$ \/ a$ e4 C$ u0 B6 F+ ~4 _
- </ul>
复制代码 v-for 迭代对象v-for 可以通过一个对象的属性来迭代数据: v-for
) B4 l5 v0 E; N8 ?& |% Z" g$ [- <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;">
9 `! L8 j& Q. ^% T - </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;">' s9 E1 z! p* R; z( }
- </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;">
) D* U, R |: s G - {{ value }}
" H% p' V1 C( n5 A1 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;">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;">
4 z. _% ^' ^+ g9 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;">
3 l4 o! y' A% k# o; b7 j7 e; N8 \ - </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;">
: c3 q1 ]- d# f6 h( x: \7 h9 U, I - # P! Q* c7 w7 I7 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;">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( B+ U, ]8 o+ F) o
- new Vue({
) `% W% t& n2 | - el: '#app',0 [3 D9 O' z- j
- data: {
- F# N% l8 i1 w; I: c3 T - object: {
/ \4 Y; o: M0 b. M1 D/ ~ - name: '菜鸟教程',7 y* ~$ B: ^% Q3 j* e
- url: 'http://www.runoob.com',
$ P7 ?* j# i: v" i) H' r - slogan: '学的不仅是技术,更是梦想!'& L' t* ?# s7 N' C6 L
- }
9 r% J4 p% k3 i/ l# U, x1 o% D - }
! l. \2 c! |% S - })
! l1 U5 R. u, }/ \/ @- ?- O7 x. 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;">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- Y: B: [( M
4 n: I; {! i9 R7 d4 \
- <div id="app"># a$ o3 L( O5 F6 O0 K' ^
- <ul>1 e& U5 i; z! z0 C' ^& ^
- <li v-for="(value, key) in object">4 p4 b3 X7 u4 ~/ |
- {{ key }} : {{ value }}# _3 _0 A& Z. g$ O6 A- x2 d' R( V
- </li>) h0 O+ ~' r n a y
- </ul>; p& m( C/ j/ q: m: d" w
- </div>
复制代码第三个参数为索引: v-for
* M$ U7 Y! T* @6 \ V0 j: m# @, F! Z! d2 q9 @. ?7 j0 B7 ?; Z5 P- y! r
- <div id="app">
8 R/ I6 {0 C: O - <ul>: g: \; ~/ L$ A2 N3 P
- <li v-for="(value, key, index) in object">
! q; }1 ^( _ E% R - {{ index }}. {{ key }} : {{ value }}4 F/ k ^, d' T3 _
- </li>4 R# {* K; ?. {
- </ul>" n7 c. T# m$ S6 N0 w7 w* n
- </div>
复制代码 v-for 迭代整数v-for 也可以循环整数 v-for3 e: D# `) R/ m/ `! {7 Y5 s" w
" p/ I) h. A! _
- <div id="app">
! r" ~4 T# K d' \( J" @4 m - <ul>0 [! ]/ n% m& S4 e- X
- <li v-for="n in 10">0 t+ |9 m; w) f8 C. q8 g
- {{ n }}
6 T1 I4 B4 o3 a: w - </li>
: W% G7 s6 I/ V6 Q; W - </ul>8 y3 d0 O# L' G- h, C3 [4 i
- </div>
复制代码
" w, i7 d2 e6 Q( n2 J0 z
: t, R' M0 k2 l) d& c) i
8 p, q7 ]6 G2 @6 R& E* a
3 C* ?; _# Y o1 M |
|