-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.html
602 lines (584 loc) · 18.8 KB
/
list.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>List</title>
<link rel="stylesheet" href="./style.css">
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/obsidian.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
</head>
<body>
<h1>List Rendering</h1>
<h2><code>v-for</code>with an Array</h2>
<p>重複<code>< li ></code>。所以將<code>v-for</code>寫在<code>< li ></code>上面。</p>
<p>印出一個array,所以將array丟進<code>for-in</code></p>
<b>HTML</b>
<pre><code class="html">< ul id="v-for-array">
< li v-for="item in items">
{{ item.message }}
< /li >
< /ul ></code></pre>
<b>Javascript</b>
<pre><code class="javascript">var example1 = new Vue({
el: '#v-for-array',
data: {
items: [
{ message: 'Foo' },
{ message: 'Bar' }
]
}
})</code></pre>
<b>Result:</b>
<ul id="v-for-array">
<li v-for="item in items">
{{ item.message }}
</li>
</ul>
<p>其中,<code>in</code>可以換成<code>of</code></p>
<pre><code class="javascript">< li v-for="item in items"> 也可以用 < li v-for="item of items"></code></pre>
<h2>v-for with an Object</h2>
<p>印出一個object成員變數,所以將object丟進<code>for-in</code></p>
<b>HTML</b>
<pre><code class="html">< ul id="v-for-object" class="demo">
< li v-for="value in object">
{{ value }}
< /li >
< /ul ></code></pre>
<b>Javascript</b>
<pre><code class="javascript">new Vue({
el: '#v-for-object',
data: {
object: {
firstName: 'John',
lastName: 'Doe',
age: 30
}
}
})</code></pre>
<b>Result:</b>
<ul id="v-for-object" class="demo">
<li v-for="value in object">
{{ value }}
</li>
</ul>
<h3>印出<code>key-value</code></h3>
<b>HTML</b>
<pre><code class="html">< div v-for="(value, key) in object">
{{ key }}: {{ value }}
< /div ></code></pre>
<b>Result:</b>
<div id="v-for-key-value">
<div v-for="(value, key) in object">
{{ key }}: {{ value }}
</div>
</div>
<h3>印出<code>index</code></h3>
<p>0起始計數。</p>
<b>HTML</b>
<pre><code class="html">< div v-for="(value, key) in object">
{{ index }}. {{ key }}: {{ value }}
< /div ></code></pre>
<b>Result:</b>
<div id="v-for-index">
<div v-for="(value, key, index) in object">
{{ index }}. {{ key }}: {{ value }}
</div>
</div>
不保證順序每次都相同。
<h2><code>key</code>的用法!!</h2>
目前對這個功能還不了解!!!有找到<a href="https://www.zhihu.com/question/61064119" target="_blank">一篇文章</a>可以參考
<div id="v-for-key">
<div v-for="(value, key, index) in object" :key = "key">
{{ index }}. {{ key }}: {{ value }}
</div>
</div>
<h2>Array Change Detection</h2>
<p>陣列經過這些<code>function</code>都會觸發更新畫面</p>
<p>修改元素,修改原陣列</p>
<ul>
<li><code>push()</code></li>
<li><code>pop()</code></li>
<li><code>shift()</code></li>
<li><code>unshift()</code></li>
<li><code>splice()</code></li>
<li><code>sort()</code></li>
<li><code>reverse()</code></li>
</ul>
<p>修改元素, 回傳新陣列</p>
<ul>
<li><code>filter()</code></li>
<li><code>concat()</code></li>
<li><code>slice()</code></li>
</ul>
<b>example</b>
<pre><code class="html">< ul id="example-1" >
< li v-for="item in items" >
{{ item.message }}
< /li >
< /ul ></code></pre>
<ul id="array-methods">
<li v-for="item in items">
{{ item.message }}
</li>
</ul>
<p>在這個例子可以打開<code>console</code>輸入<code>array_methods.items.push({ message: 'Baz' })</code>可以立即新增元素到畫面上</p>
<h3>Replacing an Array</h3>
<p>無論js是回傳新的array,還是修改原本的array,vue在渲染DOM的行為,都是高效能的。</p>
<h3>Caveats</h3>
<p class="tip">因為javascript的限制,Array有些操作方式無法觸發「Vue重新渲染」,不過在此也提供了一些解決方式。</p>
<ol>
<li>當你利用索引直接設置一個項時,例如: <code>vm.items[indexOfItem] = newValue</code></li>
<li>當你修改數組的長度時,例如: <code>vm.items.length = newLength</code></li>
</ol>
<b>1. 的解決方式</b>
<p>用<code>Vue.set()</code></p>
<pre><code class="javascript">// Vue.set
Vue.set(example1.items, indexOfItem, newValue)</code></pre>
<p>用<code>Array.prototype.splice</code></p>
<pre><code class="javascript">// Array.prototype.splice
example1.items.splice(indexOfItem, 1 , newValue)</code></pre>
<b>2. 的解決方式</b>
<p>用<code>Array.prototype.splice</code></p>
<pre><code class="javascript">// Array.prototype.splice
example1.items.splice(newLength)</code></pre>
<h2>Object Change Detection Caveats</h2>
<p>在此官方中文版文件....換章節!!</p>
<p class="question">有時,我們想要添加新屬性到已存在的物件上,好讓物件可以渲染到畫面上。(也許就是call兩支api的情況??)</p>
<p class="tip">因為javascript的限制,Object有些操作方式無法觸發「Vue重新渲染」,不過在此也提供了一些解決方式。</p>
<pre><code class="javascript">var vm = new Vue({
data: {
a: 1
}
})
// 賦值`vm.a`有反應
vm.b = 2
// 新增`vm.b`無反應</code></pre>
<p class="tip">Vue不允許動態地向已創建的實例添加新的根級別的反應屬性。<br />
但是,可以使用<code>Vue.set(object, key, value)</code>方法向嵌套對象添加反應屬性。<br />
例如,給出:</p>
<pre><code class="javascript">var obj_update = new Vue({
data: {
el: '#obj-update'
userProfile: {
name: 'Anika'
}
}
})</code></pre>
<b>example</b>
<div id="obj-update">
<div v-for="(property, key, index) in userProfile">
{{index}}, {{key}}, {{property}}
</div>
</div>
然後,想要在<code>obj_update.userProfile</code>裡加入新的變數,打開<code>console</code>輸入:
<pre><code class="javascript">Vue.set(obj_update.userProfile, 'age', 27)</code></pre>
或者在Vue物件內的methods時,用<code>vm.$set</code>取代全域的<code>Vue.set</code>。
<pre><code class="javascript">methods: {
add: function (key, value) {
this.$set(this.userProfile, key, value);
}
}</code></pre>
在<code>console</code>可以這樣輸入,新增元素到畫面。
<pre>obj_update.add('age', 27)</pre>
<p class="tip">有時,如果你想要添加數個變數,例如使用<code>Object.assign()</code>和<code>_.extend()</code>進行物件屬性的合併。<br />
但是,這樣添加的話是不會觸發更新畫面的。</p>
<pre><code class="javascript">Object.assign(obj_update.userProfile, {
age: 27,
favoriteColor: 'Vue Green'
})</code></pre>
<p>要解決這個問題,要用一個新的物件賦值</p>
<p>可以直接在<code>console</code>貼上這一段。上面的資料會動態渲染唷。</p>
<pre><code class="javascript">const newUserProfile = Object.assign({}, obj_update.userProfile, {
age: 27,
favoriteColor: 'Vue Green'
})
obj_update.userProfile = newUserProfile;</code></pre>
<h2>Displaying Filtered/Sorted Results</h2>
<p>有時,我們想要顯示 過濾/排序 的資料副本。</p>
<p>這裡有一個「顯示偶數」的例子</p>
<b>HTML</b>
<pre><code class="html">< li v-for = "n in evenNumbers" > {{ n }} < /li ></code></pre>
<b>Javascrit</b>
<pre><code class="javascript">data: {
numbers : [ 1 , 2 , 3 , 4 , 5 ]
},
computed : {
evenNumbers : function () {
return this .numbers.filter( function ( number ) {
return number % 2 === 0
})
}
}</code></pre>
<b>Result</b>
<div id="even-number">
<li v-for="n in evenNumbers" > {{ n }} </li>
</div>
可以用一個取偶數Method
<b>HTML</b>
<pre><code class="html">< li v-for="n in even(numbers)">{{ n }}< /li ></code></pre>
<b>Javascript</b>
<pre><code class="javascript">data: {
numbers: [ 1, 2, 3, 4, 5 ]
},
methods: {
even: function (numbers) {
return numbers.filter(function (number) {
return number % 2 === 0
})
}
}</code></pre>
<b>Result</b>
<div id="even-number2">
<li v-for="n in even(numbers)">{{ n }}</li>
</div>
<h2>v-for with a Range</h2>
<code>v-for</code> 可以計數整數。在此例,它可以重複tag
<pre><code class="html">< div >
< span v-for="n in 10">{{ n }} < /span >
< /div ></code></pre>
<b>渲染成</b>
<pre><code class="html">< div id="vfor-range" >
< span >1 < /span >
< span >2 < /span >
< span >3 < /span >
< span >4 < /span >
< span >5 < /span >
< span >6 < /span >
< span >7 < /span >
< span >8 < /span >
< span >9 < /span >
< span >10 < /span >
< /div ></code></pre>
<b>Result</b>
<div id="vfor-range">
<span v-for="n in 10">{{ n }} </span>
</div>
<h2>v-for on a < template ><small> <br />一次渲染多個<code>HTML</code>tag</small></h2>
<p>重複多個<code>HTML</code>tag。所以將<code>v-for</code>寫在<code>< template ></code>上面。</p>
<pre><code class="html">< ul >
< template v-for="item in items" >
< li >{{ item.msg }}< /li >
< li class="divider">< /li >
< /template >
< /ul ></code></pre>
<b>Result(用template)</b>
<ul id="vfor-template1">
<template v-for="item in items">
<li>{{ item.msg }}</li>
<li class="divider">divider</li>
</template>
</ul>
<h3>不可行的寫法</h3>
<pre><code class="html">< ul v-for="item in items" > -->不會重複
< li >{{ item.msg }}< /li > --->消失
< li class="divider">< /li > --->消失
< /ul ></code></pre>
<pre><code class="html">< ul >
< li v-for="item in items" >{{ item.msg }}< /li > -->只會重複它
< li class="divider">< /li > --->只有一個
< /ul ></code></pre>
<ul id="vfor-template2">
<li v-for="item in items">{{ item.msg }}</li>
<li class="divider">divider</li>
</ul>
<h2><code>v-for</code> with <code>v-if</code></h2>
可以條件式渲染<code>v-for</code>的內容,用起來像<code>for loop</code>一樣。
<pre><code class="html">< li v-for = "todo in todos" v-if = "!todo.isComplete" >
{{ todo }}
< /li ></code></pre>
<b>Result</b>
<div id="vfor-vif">
<li v-for="todo in todos" v-if="!todo.isComplete">
{{todo.msg}}
</li>
</div>
也可以條件渲染是否要loop,可以將<code>v-if</code>置於外部的元素,或者新加一個<code>< template ></code>放<code>v-if</code>。
<pre><code class="html">< ul v-if = "shouldRenderTodos" >
< li v-for = "todo in todos" >
{{ todo }}
< /li >
< /ul >
</code></pre>
<h2>v-for with a Component</h2>
<p>你可以直接在<code>component</code>自訂的tag上放置<code>v-for</code>,像下面這樣</p>
<pre>< my-component v-for="item in items" :key="item.id">< /my-component ></pre>
<p><b>在v2.2.0+ 之後,<code>component</code>上的<code>v-for</code>都要加上<code>key</code>。</b></p>
<p><code>component</code>有自己獨立的<code>scop</code>,要將變數傳入<code>component</code>使用,要透過<code>props</code></p>
<pre><code class="html">< my-component
v-for="(item, index) in items"
v-bind:item="item"
v-bind:index="index"
v-bind:key="item.id"
>< /my-component ></code></pre>
<p>下面提供一個實例來看看,這是一個什麼樣的機制</p>
<b>HTML</b>
<pre><code class="html">< div id="todo-list-example">
< input
v-model="newTodoText"
v-on:keyup.enter="addNewTodo"
placeholder="Add a todo"
>
< ul>
< li is="todo-item" ---------> component
v-for="(todo, index) in todos" --> todos is data of vue object
v-bind:key="todo.id"
v-bind:name="todo.title" ---------> v-bind:"name" is prop of component
v-on:remove="todos.splice(index, 1)">
< /li> ---------> component
< /ul>
< /div></code></pre>
<b>Javascript</b>
<pre><code class="javascript">Vue.component('todo-item', {
props: ['name'],
template: `
< li>
{{ name }}
< button v-on:click="$emit('remove')">X< /button>
< /li>`
})
new Vue({
el: '#todo-list-example',
data: {
newTodoText: '',
todos: [
{ id: 1, title: 'Do the dishes', },
{ id: 2, title: 'Take out the trash', },
{ id: 3, title: 'Mow the lawn' }
],
nextTodoId: 4
},
methods: {
addNewTodo: function () {
this.todos.push({
id: this.nextTodoId++,
title: this.newTodoText
})
this.newTodoText = ''
}
}
})</code></pre>
<b>Result</b>
<div id="todo-list-example">
<input
v-model="newTodoText"
v-on:keyup.enter="addNewTodo"
placeholder="Add a todo"
>
<ul>
<li
is="todo-item"
v-for="(todo, index) in todos"
v-bind:key="todo.id"
v-bind:name="todo.title"
v-on:remove="todos.splice(index, 1)"
></li>
</ul>
</div>
<script src="https://unpkg.com/vue"></script>
<script type="text/javascript">
var vfor_array = new Vue({
el: '#v-for-array',
data: {
items: [
{ message: 'Foo' },
{ message: 'Bar' }
]
}
})
var vfor_object = new Vue({
el: '#v-for-object',
data: {
object: {
firstName: 'John',
lastName: 'Doe',
age: 30
}
}
})
var vfor_keyvalue = new Vue({
el: '#v-for-key-value',
data: {
object: {
firstName: 'John',
lastName: 'Doe',
age: 30
}
}
})
var vfor_index = new Vue({
el: '#v-for-index',
data: {
object: {
firstName: 'John',
lastName: 'Doe',
age: 30
}
}
})
var vfor_key = new Vue({
el: '#v-for-key',
data: {
object: {
firstName: 'John',
lastName: 'Doe',
age: 30
}
}
})
var array_methods = new Vue({
el: '#array-methods',
data: {
items: [
{ message: 'Foo' },
{ message: 'Bar' }
]
}
})
var obj_update = new Vue({
el: '#obj-update',
data: {
userProfile: {
name: 'Anika'
}
},
methods: {
add: function (key, value) {
this.$set(this.userProfile, key, value);
}
}
})
var even_number = new Vue({
el: '#even-number',
data: {
numbers : [ 1 , 2 , 3 , 4 , 5 ]
},
computed : {
evenNumbers : function () {
return this.numbers.filter( function ( number ) {
return number % 2 === 0
})
}
}
})
var vfor_range = new Vue({
el: '#vfor-range',
data: {
numbers: [ 1, 2, 3, 4, 5 ]
},
methods: {
even: function (numbers) {
return numbers.filter(function (number) {
return number % 2 === 0
})
}
}
})
var even_number2 = new Vue({
el: '#even-number2',
data: {
numbers: [ 1, 2, 3, 4, 5 ]
},
methods: {
even: function (numbers) {
return numbers.filter(function (number) {
return number % 2 === 0
})
}
}
})
var vfor_template1 = new Vue({
el: '#vfor-template1',
data: {
items: [
{msg: 1},
{msg: 2},
{msg: 3},
]
},
methods: {
even: function (numbers) {
return numbers.filter(function (number) {
return number % 2 === 0
})
}
}
})
var vfor_template2 = new Vue({
el: '#vfor-template2',
data: {
items: [
{msg: 1},
{msg: 2},
{msg: 3},
]
},
methods: {
even: function (numbers) {
return numbers.filter(function (number) {
return number % 2 === 0
})
}
}
})
var vfor_vif = new Vue({
el: '#vfor-vif',
data: {
todos: [
{msg: 1, isComplete: true},
{msg: 2, isComplete: false},
{msg: 3, isComplete: true},
]
},
methods: {
even: function (numbers) {
return numbers.filter(function (number) {
return number % 2 === 0
})
}
}
})
Vue.component('todo-item', {
props: ['name'],
template: `
<li>
{{ name }}
<button v-on:click="$emit('remove')">X</button>
</li>`
})
new Vue({
el: '#todo-list-example',
data: {
newTodoText: '',
todos: [
{
id: 1,
title: 'Do the dishes',
},
{
id: 2,
title: 'Take out the trash',
},
{
id: 3,
title: 'Mow the lawn'
}
],
nextTodoId: 4
},
methods: {
addNewTodo: function () {
this.todos.push({
id: this.nextTodoId++,
title: this.newTodoText
})
this.newTodoText = ''
}
}
})
</script>
</body>
</html>