-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.xml
599 lines (488 loc) · 91.6 KB
/
index.xml
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
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Elegenthus's blog</title>
<link>https://Elegenthus.github.io/</link>
<description>Recent content on Elegenthus's blog</description>
<generator>Hugo -- gohugo.io</generator>
<language>zh-CN</language>
<lastBuildDate>Sun, 20 Nov 2016 16:19:56 +0800</lastBuildDate>
<atom:link href="https://Elegenthus.github.io/index.xml" rel="self" type="application/rss+xml" />
<item>
<title>VueDirectivesTest</title>
<link>https://elegenthus.github.io/post/VueDirectivesTest/</link>
<pubDate>Sun, 20 Nov 2016 16:19:56 +0800</pubDate>
<guid>https://elegenthus.github.io/post/VueDirectivesTest/</guid>
<description>
<h1 id="使用vue-directive封装dom操作:8fb70ceb80e0e159e41b00203ce25900">使用Vue Directive封装DOM操作</h1>
<p>这篇文章是关于Vue Directive的介绍,后面会介绍一个使用的小例子。</p>
<hr />
<h2 id="1-vue-directive的简介-br:8fb70ceb80e0e159e41b00203ce25900">1.Vue Directive的简介 <br></h2>
<p>directive在Vue中十分常见,Vue提供的许多指令如 <code>v-on</code> 、 <code>v-show</code> 使得Vue的功能更加强大,可是仍然有一些操作指令是Vue没有提供的,不过Vue提供了自定义directive的属性方法来满足这一需求。其实,Vue Directive就是一种特殊的HTML元素属性。<br>
Vue提供了两中注册方法,一种是全局注册,在Vue.directive中定义组件的名字和相关指令操作</p>
<div class="highlight" style="background: #272822"><pre style="line-height: 125%"><span></span> <span style="color: #a6e22e">Vue</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">directive</span><span style="color: #f8f8f2">(</span><span style="color: #e6db74">&#39;指令的名字&#39;</span><span style="color: #f8f8f2">,</span> <span style="color: #f8f8f2">{</span>
<span style="color: #75715e">//一些option,提供许多hook function 定义directive的具体操作,如inserted、bind...</span>
<span style="color: #a6e22e">inserted</span><span style="color: #f92672">:</span> <span style="color: #66d9ef">function</span> <span style="color: #f8f8f2">(</span><span style="color: #a6e22e">el</span><span style="color: #f8f8f2">)</span> <span style="color: #f8f8f2">{</span>
<span style="color: #75715e">//指令插入元素后的操作</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #f8f8f2">})</span>
</pre></div>
<p>另一种是局部注册,在对应组件的directives属性里注册组件名,传入指令的option对象</p>
<div class="highlight" style="background: #272822"><pre style="line-height: 125%"><span></span><span style="color: #a6e22e">directives</span><span style="color: #f92672">:</span> <span style="color: #f8f8f2">{</span>
<span style="color: #a6e22e">name</span><span style="color: #f92672">:</span> <span style="color: #f8f8f2">{</span> <span style="color: #75715e">//指令的名字</span>
<span style="color: #75715e">// 指令的定义</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #f8f8f2">}</span>
</pre></div>
<p>在定义时,Vue提供了许多hook function,例如 <code>inserted</code> 、<code>bind</code>等,<code>bind</code>是只会在绑定的对象节点被插入父节点时调用一次的函数,与<code>unbind</code>相对应,<code>unbind</code>将也只会在元素被解除绑定时调用。<br></p>
<div class="highlight" style="background: #272822"><pre style="line-height: 125%"><span></span><span style="color: #a6e22e">Vue</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">directive</span><span style="color: #f8f8f2">(</span><span style="color: #e6db74">&#39;指令的名字&#39;</span><span style="color: #f8f8f2">,</span> <span style="color: #f8f8f2">{</span>
<span style="color: #a6e22e">bind</span><span style="color: #f92672">:</span> <span style="color: #66d9ef">function</span><span style="color: #f8f8f2">(</span><span style="color: #a6e22e">el</span><span style="color: #f8f8f2">,</span> <span style="color: #a6e22e">binding</span><span style="color: #f8f8f2">,</span> <span style="color: #a6e22e">vnode</span><span style="color: #960050; background-color: #1e0010">,</span><span style="color: #a6e22e">oldVnode</span><span style="color: #f8f8f2">)</span> <span style="color: #f8f8f2">{</span>
<span style="color: #75715e">//绑定指令时调用</span>
<span style="color: #f8f8f2">},</span>
<span style="color: #a6e22e">unbind</span><span style="color: #f92672">:</span> <span style="color: #66d9ef">function</span><span style="color: #f8f8f2">()</span> <span style="color: #f8f8f2">{</span>
<span style="color: #75715e">//指令解除绑定时调用</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #f8f8f2">})</span>
</pre></div>
<p><code>bind</code>函数中的<code>binding</code>,是一个包含了众多属性的对象,关于它的所有的性质的介绍可以自行查看<a href="https://vuejs.org/v2/guide/custom-directive.html">官方文档</a> ,比较常用的一个属性<code>binding.value</code>,与其对应的是在指令中传入的字符串,Vue会自动解析这个值并执行。与此相对应的另一个属性<code>binding.expression</code>里的值则就是原始值。<br></p>
<h2 id="2-为什么要使用vuedirective-br:8fb70ceb80e0e159e41b00203ce25900">2.为什么要使用VueDirective <br></h2>
<p>有同学可能会问,我在Vue实例的方法中写DOM操作不可以吗?那为什么使用Vue Directive来封装DOM操作呢?<br>
这是因为,为了实现View和ViewModel的分离,我们必须封装DOM操作,View层负责页面上的显示,ViewModel层负责改变操作数据,由于Vue是数据驱动的,属于ViewModel层,那么其中就不应该出现View层上的DOM操作,而且,使用Vue Directive是和DOM元素的创建、销毁绑定的。Vue Directive的生命周期方法能让我们更优雅的去在合适的时机进行DOM的操作。而在ViewModel里则没有和DOM元素对应的方法。因为Vue Directive是属于View层面的,所以DOM操作应该被封装在Vue Directive里而不是出现在Vue实例中。 <br></p>
<h2 id="3-demo-自制scroll指令-br:8fb70ceb80e0e159e41b00203ce25900">3. Demo:自制scroll指令 <br></h2>
<p>接下来看一个具体使用Vue Directive封装DOM操作的scroll事件的例子,在这个例子中需求是在用户滑动到页面的底端时请求更多数据。<br></p>
<h3 id="3-1-首先将dom操作封装到指令的option中-br:8fb70ceb80e0e159e41b00203ce25900">3.1 首先将DOM操作封装到指令的option中 <br></h3>
<p><div class="highlight" style="background: #272822"><pre style="line-height: 125%"><span></span><span style="color: #66d9ef">let</span> <span style="color: #a6e22e">scrollCallback</span> <span style="color: #f92672">=</span> <span style="color: #66d9ef">function</span><span style="color: #f8f8f2">(</span><span style="color: #a6e22e">callback</span><span style="color: #f8f8f2">)</span> <span style="color: #f8f8f2">{</span>
<span style="color: #66d9ef">if</span> <span style="color: #f8f8f2">(document.</span><span style="color: #a6e22e">body</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">scrollHeight</span> <span style="color: #f92672">&lt;</span> <span style="color: #ae81ff">1000</span><span style="color: #f8f8f2">)</span> <span style="color: #f8f8f2">{</span>
<span style="color: #66d9ef">return</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #66d9ef">if</span> <span style="color: #f8f8f2">(document.</span><span style="color: #a6e22e">body</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">scrollHeight</span> <span style="color: #f92672">-</span> <span style="color: #f8f8f2">window.</span><span style="color: #a6e22e">scrollY</span> <span style="color: #f92672">-</span> <span style="color: #ae81ff">100</span> <span style="color: #f92672">&lt;=</span> <span style="color: #f8f8f2">document.</span><span style="color: #a6e22e">body</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">clientHeight</span><span style="color: #f8f8f2">)</span> <span style="color: #f8f8f2">{</span>
<span style="color: #a6e22e">callback</span><span style="color: #f8f8f2">()</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #66d9ef">let</span> <span style="color: #a6e22e">callBackWarpped</span> <span style="color: #75715e">// 新变量 保存引用</span>
<span style="color: #66d9ef">export</span> <span style="color: #66d9ef">default</span> <span style="color: #f8f8f2">{</span>
<span style="color: #a6e22e">bind</span><span style="color: #f92672">:</span> <span style="color: #66d9ef">function</span><span style="color: #f8f8f2">(</span><span style="color: #a6e22e">el</span><span style="color: #f8f8f2">,</span> <span style="color: #a6e22e">binding</span><span style="color: #f8f8f2">,</span> <span style="color: #a6e22e">vnode</span><span style="color: #f8f8f2">)</span> <span style="color: #f8f8f2">{</span>
<span style="color: #a6e22e">callBackWarpped</span> <span style="color: #f92672">=</span> <span style="color: #a6e22e">scrollCallback</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">bind</span><span style="color: #f8f8f2">({},</span> <span style="color: #a6e22e">binding</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">value</span><span style="color: #f8f8f2">)</span>
<span style="color: #f8f8f2">window.</span><span style="color: #a6e22e">addEventListener</span><span style="color: #f8f8f2">(</span><span style="color: #e6db74">&quot;scroll&quot;</span><span style="color: #f8f8f2">,</span> <span style="color: #a6e22e">callBackWarpped</span><span style="color: #f8f8f2">)</span>
<span style="color: #f8f8f2">},</span>
<span style="color: #a6e22e">unbind</span><span style="color: #f92672">:</span> <span style="color: #66d9ef">function</span><span style="color: #f8f8f2">()</span> <span style="color: #f8f8f2">{</span>
<span style="color: #f8f8f2">window.</span><span style="color: #a6e22e">removeEventListener</span><span style="color: #f8f8f2">(</span><span style="color: #e6db74">&quot;scroll&quot;</span><span style="color: #f8f8f2">,</span> <span style="color: #a6e22e">callBackWarpped</span><span style="color: #f8f8f2">)</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #f8f8f2">}</span>
</pre></div>
</p>
<p>首先需要监听页面的滚动,如果触发了scroll事件那么就要执行回调函数,由于在解除绑定的时候也要将监听事件从window上移除,所以必须给回调函数取一个名字,例如本例中的<code>scrollCallback</code>,并在unbind函数中将监听移除,因此<code>scrollCallBack</code>的具体定义应在对象外执行。<br>
并且回调函数应该在页面滑动到底端时才执行,也就是说不能马上执行 <code>binding.value</code>,怎样实现这一点呢?可以将<code>bind.value</code>作为函数的参数传进<code>scrollCallback</code>,先判断,然后在满足条件时调用<code>binding.value</code> 。<br></p>
<h3 id="3-2-在组件里import一个directive-br:8fb70ceb80e0e159e41b00203ce25900">3.2 在组件里import一个directive <br></h3>
<p><div class="highlight" style="background: #272822"><pre style="line-height: 125%"><span></span><span style="color: #66d9ef">import</span> <span style="color: #a6e22e">scrollDirective</span> <span style="color: #a6e22e">from</span> <span style="color: #e6db74">&#39;../../directives/scroll&#39;</span>
<span style="color: #75715e">//import 指令的option的名字 from &#39;指令option的位置&#39;</span>
</pre></div>
</p>
<p>使用import将directive引入组件。 <br></p>
<h3 id="3-3-在组件的directives属性中注册这个指令-值为scroll:8fb70ceb80e0e159e41b00203ce25900">3.3 在组件的directives属性中注册这个指令,值为scroll</h3>
<p><div class="highlight" style="background: #272822"><pre style="line-height: 125%"><span></span><span style="color: #a6e22e">directives</span><span style="color: #f92672">:</span> <span style="color: #f8f8f2">{</span>
<span style="color: #a6e22e">scroll</span><span style="color: #f92672">:</span> <span style="color: #a6e22e">scrollDirective</span>
<span style="color: #75715e">//指令的名字:指令的option的名字</span>
<span style="color: #f8f8f2">},</span>
</pre></div>
</p>
<p>将引入的组件注册到scroll上。 <br></p>
<h3 id="3-4-在相应元素上加上directive指令-br:8fb70ceb80e0e159e41b00203ce25900">3.4 在相应元素上加上directive指令<br></h3>
<p><div class="highlight" style="background: #272822"><pre style="line-height: 125%"><span></span><span style="color: #a6e22e">v</span><span style="color: #f92672">-</span><span style="color: #a6e22e">scroll</span> <span style="color: #f92672">=</span> <span style="color: #e6db74">&quot;onScroll&quot;</span>
<span style="color: #75715e">//v-&#39;指令的名字&#39; = &quot;回调函数&quot;</span>
</pre></div>
</p>
<p>用注册好的名字在相对应的元素上加入指令 。<br></p>
<h3 id="3-5-编写directive指令中的函数-br:8fb70ceb80e0e159e41b00203ce25900">3.5 编写directive指令中的函数<br></h3>
<p>最后在method中编写<code>onScroll</code>函数,也就是上文中对应的<code>binding.value</code>,在这个函数中我们将会请求更多数据,而Vue就会执行相应<code>v-scroll</code>中的值。 <br><br></p>
<hr />
<p>具体的Demo请戳<a href="https://github.com/Elegenthus/scrollDemo/tree/gh-pages">这里</a></p>
</description>
</item>
<item>
<title>hackathon_Match(3)</title>
<link>https://elegenthus.github.io/post/hackathon_Match%283%29/</link>
<pubDate>Thu, 03 Nov 2016 19:54:22 +0800</pubDate>
<guid>https://elegenthus.github.io/post/hackathon_Match%283%29/</guid>
<description><p><a href="http://match.muxixyz.com">Match</a></p>
<hr />
<p>恭喜你,来到了我们的result页面,我们很快就能看到我们的match分数啦~是不是很期待?<br>
我们先来公布一份超级有爱的match结果~<br>
<img src="../../img/match3.png" alt="" />
<br>
大家可以看到我们的页面上有显示相对应应用中的共同项<br>
这里我决定只显示有共同项的应用版块,这里采用的方法是在判断每一个版块上所返回的字符串是否为空,代码如下,若info.douban.book为空,则v-show=&ldquo;false&rdquo;,该板块就不会显示</p>
<div class="highlight" style="background: #272822"><pre style="line-height: 125%"><span></span><span style="color: #f8f8f2">&lt;</span><span style="color: #f92672">div</span> <span style="color: #a6e22e">class</span><span style="color: #f92672">=</span><span style="color: #e6db74">&quot;common&quot;</span> <span style="color: #a6e22e">v-show </span><span style="color: #f92672">=</span> <span style="color: #e6db74">&quot;info.douban.book&quot;</span><span style="color: #f8f8f2">&gt;</span>
<span style="color: #f8f8f2">&lt;</span><span style="color: #f92672">div</span> <span style="color: #a6e22e">class</span><span style="color: #f92672">=</span><span style="color: #e6db74">&quot;common_t&quot;</span><span style="color: #f8f8f2">&gt;</span>共同看过的小说:<span style="color: #f8f8f2">&lt;/</span><span style="color: #f92672">div</span><span style="color: #f8f8f2">&gt;</span>
<span style="color: #f8f8f2">&lt;</span><span style="color: #f92672">div</span> <span style="color: #a6e22e">class</span><span style="color: #f92672">=</span><span style="color: #e6db74">&quot;common_t&quot;</span><span style="color: #f8f8f2">&gt;</span>
{{info.douban.book}}
<span style="color: #f8f8f2">&lt;/</span><span style="color: #f92672">div</span><span style="color: #f8f8f2">&gt;</span>
<span style="color: #f8f8f2">&lt;/</span><span style="color: #f92672">div</span><span style="color: #f8f8f2">&gt;</span>
<span style="color: #f8f8f2">&lt;/</span><span style="color: #f92672">div</span><span style="color: #f8f8f2">&gt;</span>
</pre></div>
<p>好的,接下来就是我们显示match指数的的百分比图了,动态效果请戳<a href="https://jsbin.com/lafiqatocu/edit?html,css,output">piechart</a></p>
<p><div class="highlight" style="background: #272822"><pre style="line-height: 125%"><span></span><span style="color: #f8f8f2">&lt;</span><span style="color: #f92672">template</span><span style="color: #f8f8f2">&gt;</span>
...
<span style="color: #f8f8f2">&lt;</span><span style="color: #f92672">svg</span> <span style="color: #a6e22e">viewBox </span><span style="color: #f92672">=</span> <span style="color: #e6db74">&quot;0 0 32 32&quot;</span><span style="color: #f8f8f2">&gt;</span>
<span style="color: #f8f8f2">&lt;</span><span style="color: #f92672">circle</span> <span style="color: #a6e22e">class</span><span style="color: #f92672">=</span><span style="color: #e6db74">&quot;circle&quot;</span> <span style="color: #a6e22e">r </span><span style="color: #f92672">=</span> <span style="color: #e6db74">&quot;16&quot;</span> <span style="color: #a6e22e">cx </span><span style="color: #f92672">=</span> <span style="color: #e6db74">&quot;16&quot;</span> <span style="color: #a6e22e">cy </span><span style="color: #f92672">=</span><span style="color: #e6db74">&quot;16&quot;</span> <span style="color: #a6e22e">v-bind:style </span><span style="color: #f92672">=</span> <span style="color: #e6db74">&quot;{strokeDasharray:Math.floor(score) + &#39; 100&#39;}&quot;</span><span style="color: #f8f8f2">&gt;&lt;/</span><span style="color: #f92672">circle</span><span style="color: #f8f8f2">&gt;</span>
<span style="color: #f8f8f2">&lt;/</span><span style="color: #f92672">svg</span><span style="color: #f8f8f2">&gt;</span>
...
<span style="color: #f8f8f2">&lt;/</span><span style="color: #f92672">template</span><span style="color: #f8f8f2">&gt;</span>
<span style="color: #f8f8f2">&lt;</span><span style="color: #f92672">style</span><span style="color: #f8f8f2">&gt;</span>
<span style="color: #f92672">...</span>
<span style="color: #f92672">svg</span><span style="color: #f8f8f2">{</span>
<span style="color: #f8f8f2">width</span><span style="color: #f92672">:</span> <span style="color: #ae81ff">100px</span><span style="color: #f8f8f2">;</span>
<span style="color: #f8f8f2">height</span><span style="color: #f92672">:</span> <span style="color: #ae81ff">100px</span><span style="color: #f8f8f2">;</span>
<span style="color: #f8f8f2">transform</span><span style="color: #f92672">:</span> <span style="color: #f8f8f2">rotate(</span><span style="color: #ae81ff">-90</span><span style="color: #f8f8f2">deg);</span>
<span style="color: #f8f8f2">border</span><span style="color: #f92672">-</span><span style="color: #f8f8f2">radius</span><span style="color: #f92672">:</span> <span style="color: #ae81ff">50%</span><span style="color: #f8f8f2">;</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #a6e22e">.circle</span><span style="color: #f8f8f2">{</span>
<span style="color: #f8f8f2">fill</span><span style="color: #f92672">:</span><span style="color: #f8f8f2">transparent;</span>
<span style="color: #f8f8f2">stroke</span><span style="color: #f92672">:</span><span style="color: #ae81ff">#e1484b</span><span style="color: #f8f8f2">;</span>
<span style="color: #f8f8f2">stroke</span><span style="color: #f92672">-</span><span style="color: #f8f8f2">width</span><span style="color: #f92672">:</span><span style="color: #ae81ff">6px</span><span style="color: #f8f8f2">;</span>
<span style="color: #f8f8f2">animation</span><span style="color: #f92672">:</span> <span style="color: #f8f8f2">fillup</span> <span style="color: #ae81ff">1s</span> <span style="color: #f8f8f2">ease</span><span style="color: #f92672">-</span><span style="color: #f8f8f2">out</span> <span style="color: #f8f8f2">forwards;</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #66d9ef">@keyframes</span> <span style="color: #f92672">fillup</span><span style="color: #f8f8f2">{</span>
<span style="color: #f92672">from</span><span style="color: #f8f8f2">{stroke</span><span style="color: #f92672">-</span><span style="color: #f8f8f2">dasharray</span><span style="color: #f92672">:</span> <span style="color: #ae81ff">0</span> <span style="color: #ae81ff">100</span><span style="color: #f8f8f2">;}</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #f92672">...</span>
<span style="color: #f8f8f2">&lt;/</span><span style="color: #f92672">style</span><span style="color: #f8f8f2">&gt;</span>
</pre></div>
</p>
<p>我们将采用svg标签来画这个百分比图,用stroke属性来实现动态变化效果
<br>
svg的viewBox顾名思义就是视区盒子的意思,它覆盖整个svg图,我们在style中定义svg是一个半径为50px的圆,那么viewBox = &ldquo;0 0 32 32&rdquo;只是内部将此圆的半径长分为32份,而circle中的r代表此圆的半径,相对于viewBox而言的16,既是整个svg图的一半,cx,cy是其圆心坐标,处于svg图的正中心。<br>
接下来看它的动态效果如何实现,这里我们仍然需要用<a href="http://www.runoob.com/svg/svg-stroke.html">svg的stroke</a>属性,stroke会沿着svg图像(此处为一个半径为50的圆)的边界划出一条线,我们再用animation属性使其产生变化的效果。这样一个piechart就完成啦<br></p>
<hr />
<p>好啦,你还等什么,赶紧去寻找你和你的TA的相同点吧~</p>
</description>
</item>
<item>
<title>hackathon_Match(2)</title>
<link>https://elegenthus.github.io/post/hackathon_Match%282%29/</link>
<pubDate>Wed, 02 Nov 2016 18:46:18 +0800</pubDate>
<guid>https://elegenthus.github.io/post/hackathon_Match%282%29/</guid>
<description><p><a href="http://match.muxixyz.com">Match</a></p>
<hr />
<p>接下来我们来一起看index.vue,先展示一下我们的成品页面
<img src="../../img/match1.png" alt="" />
<img src="../../img/match2.png" alt="" />
很漂亮对不对?<br>
第一个页面的具体样式我们这里不做赘述,我们怎么从第一个页面切换到第二个页面呢?</p>
<div class="highlight" style="background: #272822"><pre style="line-height: 125%"><span></span><span style="color: #f8f8f2">&lt;</span><span style="color: #f92672">div</span> <span style="color: #a6e22e">class</span><span style="color: #f92672">=</span><span style="color: #e6db74">&quot;main item&quot;</span> <span style="color: #a6e22e">v-bind:class</span><span style="color: #f92672">=</span><span style="color: #e6db74">&quot;{ mainNext : onNext}&quot;</span> <span style="color: #a6e22e">v-on:mousewheel</span><span style="color: #f92672">=</span><span style="color: #e6db74">&quot;scrollNext&quot;</span><span style="color: #f8f8f2">&gt;</span>
...
<span style="color: #f8f8f2">&lt;/</span><span style="color: #f92672">div</span><span style="color: #f8f8f2">&gt;</span>
<span style="color: #f8f8f2">&lt;</span><span style="color: #f92672">div</span> <span style="color: #a6e22e">class</span><span style="color: #f92672">=</span><span style="color: #e6db74">&quot;input item&quot;</span> <span style="color: #a6e22e">v-bind:class</span><span style="color: #f92672">=</span><span style="color: #e6db74">&quot;{ inputNext : onNext}&quot;</span><span style="color: #f8f8f2">&gt;</span>
...
<span style="color: #f8f8f2">&lt;/</span><span style="color: #f92672">div</span><span style="color: #f8f8f2">&gt;</span>
</pre></div>
<p>第一个div main中装的是第一个页面的内容,第二个div input装的是第二个页面的内容,我们用v-bind绑定选择器,并设置一个状态值onNext,当onNext为true的时候便给两个div加上相应的class,我们可以利用这一性质设置animation点击产生滑动特效,代码如下:</p>
<div class="highlight" style="background: #272822"><pre style="line-height: 125%"><span></span><span style="color: #a6e22e">.mainNext</span><span style="color: #f8f8f2">{</span>
<span style="color: #f8f8f2">animation</span><span style="color: #f92672">:</span> <span style="color: #f8f8f2">main_leave</span> <span style="color: #ae81ff">2s</span> <span style="color: #f8f8f2">ease</span><span style="color: #f92672">-</span><span style="color: #f8f8f2">in</span><span style="color: #f92672">-</span><span style="color: #f8f8f2">out;</span>
<span style="color: #f8f8f2">opacity</span><span style="color: #f92672">:</span> <span style="color: #ae81ff">0</span><span style="color: #f8f8f2">;</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #66d9ef">@keyframes</span> <span style="color: #f92672">main_leave</span> <span style="color: #f8f8f2">{</span>
<span style="color: #f92672">from</span><span style="color: #f8f8f2">{</span>
<span style="color: #f8f8f2">transform</span><span style="color: #f92672">:</span> <span style="color: #f8f8f2">translateY(</span><span style="color: #ae81ff">0</span><span style="color: #f8f8f2">);</span>
<span style="color: #f8f8f2">opacity</span><span style="color: #f92672">:</span> <span style="color: #ae81ff">1</span><span style="color: #f8f8f2">;</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #f92672">to</span><span style="color: #f8f8f2">{</span>
<span style="color: #f8f8f2">transform</span><span style="color: #f92672">:</span> <span style="color: #f8f8f2">translateY(</span><span style="color: #ae81ff">-100%</span><span style="color: #f8f8f2">);</span>
<span style="color: #f8f8f2">opacity</span><span style="color: #f92672">:</span> <span style="color: #ae81ff">0</span><span style="color: #f8f8f2">;</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #a6e22e">.inputNext</span><span style="color: #f8f8f2">{</span>
<span style="color: #f8f8f2">animation</span><span style="color: #f92672">:</span> <span style="color: #f8f8f2">input_show</span> <span style="color: #ae81ff">2s</span> <span style="color: #f8f8f2">ease</span><span style="color: #f92672">-</span><span style="color: #f8f8f2">in</span><span style="color: #f92672">-</span><span style="color: #f8f8f2">out;</span>
<span style="color: #f8f8f2">z-index</span><span style="color: #f92672">:</span> <span style="color: #ae81ff">3</span><span style="color: #f8f8f2">;</span>
<span style="color: #f8f8f2">opacity</span><span style="color: #f92672">:</span> <span style="color: #ae81ff">1</span><span style="color: #f8f8f2">;</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #66d9ef">@keyframes</span> <span style="color: #f92672">input_show</span> <span style="color: #f8f8f2">{</span>
<span style="color: #f92672">from</span><span style="color: #f8f8f2">{</span>
<span style="color: #f8f8f2">opacity</span><span style="color: #f92672">:</span> <span style="color: #ae81ff">0</span><span style="color: #f8f8f2">;</span>
<span style="color: #f8f8f2">transform</span><span style="color: #f92672">:</span> <span style="color: #f8f8f2">translateY(</span><span style="color: #ae81ff">100%</span><span style="color: #f8f8f2">);</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #f92672">to</span><span style="color: #f8f8f2">{</span>
<span style="color: #f8f8f2">opacity</span><span style="color: #f92672">:</span> <span style="color: #ae81ff">1</span><span style="color: #f8f8f2">;</span>
<span style="color: #f8f8f2">transform</span><span style="color: #f92672">:</span> <span style="color: #f8f8f2">translateY(</span><span style="color: #ae81ff">0</span><span style="color: #f8f8f2">);</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #f8f8f2">}</span>
</pre></div>
<p>我们可以实现点击下翻按钮产生滑动特效,在放有下翻图标的div上使用v-on监听click事件,绑定next函数,将onNext改为true</p>
<p><div class="highlight" style="background: #272822"><pre style="line-height: 125%"><span></span><span style="color: #a6e22e">methods</span><span style="color: #f92672">:</span><span style="color: #f8f8f2">{</span>
<span style="color: #a6e22e">next</span><span style="color: #f92672">:</span> <span style="color: #66d9ef">function</span> <span style="color: #f8f8f2">(</span><span style="color: #a6e22e">event</span><span style="color: #f8f8f2">)</span> <span style="color: #f8f8f2">{</span>
<span style="color: #66d9ef">this</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">onNext</span> <span style="color: #f92672">=</span> <span style="color: #66d9ef">true</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #f8f8f2">...</span>
<span style="color: #f8f8f2">}</span>
</pre></div>
</p>
<p>若想实现滑动页面便能翻页的特效,就在main中监听mousewheel事件,若检测到用户滑动则触发scrollNext函数</p>
<p><div class="highlight" style="background: #272822"><pre style="line-height: 125%"><span></span><span style="color: #a6e22e">scrollNext</span><span style="color: #f92672">:</span><span style="color: #66d9ef">function</span><span style="color: #f8f8f2">(</span><span style="color: #a6e22e">e</span><span style="color: #f8f8f2">){</span>
<span style="color: #66d9ef">if</span> <span style="color: #f8f8f2">(</span><span style="color: #66d9ef">this</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">onNext</span><span style="color: #f8f8f2">)</span> <span style="color: #f8f8f2">{</span>
<span style="color: #66d9ef">return</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #66d9ef">this</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">onNext</span> <span style="color: #f92672">=</span> <span style="color: #66d9ef">true</span>
<span style="color: #f8f8f2">}</span>
</pre></div>
</p>
<p>注意到我设置了一个if语句,判断onNext是否为true,若为否则将其设置为true,这是为了确保只会触发一次滑动事件
<br>
好的接下来我们通过滑动来到了第二个页面,关于第二个页面这次我新学到的一招是在父元素上设置text-align: justify;让其子元素水平居中,不过这一方法只能让处于非最后一行的子元素并列显示,因此我们还要在父元素中需居中的子元素后人为加上一个div</p>
<p><div class="highlight" style="background: #272822"><pre style="line-height: 125%"><span></span><span style="color: #f8f8f2">&lt;</span><span style="color: #f92672">div</span> <span style="color: #a6e22e">class</span><span style="color: #f92672">=</span><span style="color: #e6db74">&quot;text_align_fix&quot;</span><span style="color: #f8f8f2">&gt;&lt;/</span><span style="color: #f92672">div</span><span style="color: #f8f8f2">&gt;</span>
.text_align_fix{
display: inline-block;
width: 100%;
height: 0;
overflow: hidden;
}
</pre></div>
</p>
<p>接下来我们来看每个输入框的数据如何传给后台,其实很简单,是使用vue的v-model绑定相应的数据,然后使用fetch发送post给后台,我们以网易云音乐的数据传送为例</p>
<p><div class="highlight" style="background: #272822"><pre style="line-height: 125%"><span></span><span style="color: #f8f8f2">&lt;</span><span style="color: #f92672">template</span><span style="color: #f8f8f2">&gt;</span>
<span style="color: #75715e">&lt;!-- 用v-model绑定数据 --&gt;</span>
<span style="color: #f8f8f2">&lt;</span><span style="color: #f92672">input</span> <span style="color: #a6e22e">type</span><span style="color: #f92672">=</span><span style="color: #e6db74">&quot;text&quot;</span> <span style="color: #a6e22e">placeholder</span><span style="color: #f92672">=</span><span style="color: #e6db74">&quot;你的ID&quot;</span> <span style="color: #a6e22e">v-model </span><span style="color: #f92672">=</span> <span style="color: #e6db74">&quot;wangyiId1&quot;</span><span style="color: #f8f8f2">&gt;</span>
<span style="color: #f8f8f2">&lt;</span><span style="color: #f92672">input</span> <span style="color: #a6e22e">type</span><span style="color: #f92672">=</span><span style="color: #e6db74">&quot;text&quot;</span> <span style="color: #a6e22e">placeholder</span><span style="color: #f92672">=</span><span style="color: #e6db74">&quot;TA的ID&quot;</span> <span style="color: #a6e22e">v-model </span><span style="color: #f92672">=</span> <span style="color: #e6db74">&quot;wangyiId2&quot;</span><span style="color: #f8f8f2">&gt;</span>
...
<span style="color: #75715e">&lt;!-- 绑定 点击触发count函数事件 --&gt;</span>
<span style="color: #f8f8f2">&lt;</span><span style="color: #f92672">button</span> <span style="color: #a6e22e">class</span><span style="color: #f92672">=</span><span style="color: #e6db74">&quot;button match_button&quot;</span> <span style="color: #a6e22e">v-on:click </span><span style="color: #f92672">=</span> <span style="color: #e6db74">&quot;count&quot;</span><span style="color: #f8f8f2">&gt;</span>MATCH<span style="color: #f8f8f2">&lt;/</span><span style="color: #f92672">button</span><span style="color: #f8f8f2">&gt;</span>
...
<span style="color: #f8f8f2">&lt;/</span><span style="color: #f92672">template</span><span style="color: #f8f8f2">&gt;</span>
<span style="color: #f8f8f2">&lt;</span><span style="color: #f92672">script</span><span style="color: #f8f8f2">&gt;</span>
<span style="color: #66d9ef">import</span> <span style="color: #e6db74">&#39;whatwg-fetch&#39;</span><span style="color: #f8f8f2">;</span>
<span style="color: #66d9ef">export</span> <span style="color: #66d9ef">default</span> <span style="color: #f8f8f2">{</span>
<span style="color: #a6e22e">name</span><span style="color: #f92672">:</span><span style="color: #e6db74">&quot;index&quot;</span><span style="color: #f8f8f2">,</span>
<span style="color: #a6e22e">data</span> <span style="color: #f8f8f2">(){</span>
<span style="color: #66d9ef">return</span> <span style="color: #f8f8f2">{</span>
<span style="color: #a6e22e">onNext</span><span style="color: #f92672">:</span><span style="color: #66d9ef">false</span><span style="color: #f8f8f2">,</span> <span style="color: #75715e">// 初始化设置产生滑动效果的状态位为false</span>
<span style="color: #a6e22e">isloading</span><span style="color: #f92672">:</span><span style="color: #66d9ef">false</span><span style="color: #f8f8f2">,</span> <span style="color: #75715e">//初始化页面加载效果的状态位为false(后文会提到)</span>
<span style="color: #a6e22e">wangyiId1</span><span style="color: #f92672">:</span> <span style="color: #e6db74">&quot;&quot;</span><span style="color: #f8f8f2">,</span> <span style="color: #75715e">//定义相对应的变量用于存储数据</span>
<span style="color: #a6e22e">wangyiId2</span><span style="color: #f92672">:</span> <span style="color: #e6db74">&quot;&quot;</span><span style="color: #f8f8f2">,</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #f8f8f2">},</span>
<span style="color: #a6e22e">methods</span><span style="color: #f92672">:</span><span style="color: #f8f8f2">{</span>
<span style="color: #a6e22e">next</span><span style="color: #f92672">:</span> <span style="color: #66d9ef">function</span> <span style="color: #f8f8f2">(</span><span style="color: #a6e22e">event</span><span style="color: #f8f8f2">)</span> <span style="color: #f8f8f2">{</span>
<span style="color: #66d9ef">this</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">onNext</span> <span style="color: #f92672">=</span> <span style="color: #66d9ef">true</span>
<span style="color: #f8f8f2">},</span>
<span style="color: #a6e22e">count</span><span style="color: #f92672">:</span><span style="color: #66d9ef">function</span><span style="color: #f8f8f2">(</span><span style="color: #a6e22e">event</span><span style="color: #f8f8f2">){</span>
<span style="color: #66d9ef">this</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">isloading</span> <span style="color: #f92672">=</span> <span style="color: #66d9ef">true</span>
<span style="color: #a6e22e">fetch</span><span style="color: #f8f8f2">(</span><span style="color: #e6db74">&#39;/getScore/&#39;</span><span style="color: #f8f8f2">,</span> <span style="color: #f8f8f2">{</span> <span style="color: #75715e">//发送post请求</span>
<span style="color: #a6e22e">method</span><span style="color: #f92672">:</span> <span style="color: #e6db74">&#39;POST&#39;</span><span style="color: #f8f8f2">,</span>
<span style="color: #a6e22e">headers</span><span style="color: #f92672">:</span> <span style="color: #f8f8f2">{</span>
<span style="color: #e6db74">&#39;Accept&#39;</span><span style="color: #f92672">:</span> <span style="color: #e6db74">&#39;application/json&#39;</span><span style="color: #f8f8f2">,</span>
<span style="color: #e6db74">&#39;Content-Type&#39;</span><span style="color: #f92672">:</span> <span style="color: #e6db74">&#39;application/json&#39;</span>
<span style="color: #f8f8f2">},</span>
<span style="color: #a6e22e">body</span><span style="color: #f92672">:</span> <span style="color: #a6e22e">JSON</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">stringify</span><span style="color: #f8f8f2">({</span>
<span style="color: #a6e22e">wangyiId1</span><span style="color: #f92672">:</span> <span style="color: #66d9ef">this</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">wangyiId1</span><span style="color: #f8f8f2">,</span>
<span style="color: #a6e22e">wangyiId2</span><span style="color: #f92672">:</span> <span style="color: #66d9ef">this</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">wangyiId2</span>
<span style="color: #f8f8f2">})</span>
<span style="color: #f8f8f2">})</span>
<span style="color: #f8f8f2">.</span><span style="color: #a6e22e">then</span><span style="color: #f8f8f2">(</span> <span style="color: #a6e22e">res</span> <span style="color: #f92672">=&gt;</span> <span style="color: #f8f8f2">{</span>
<span style="color: #66d9ef">return</span> <span style="color: #a6e22e">res</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">json</span><span style="color: #f8f8f2">()</span>
<span style="color: #f8f8f2">})</span>
<span style="color: #f8f8f2">.</span><span style="color: #a6e22e">then</span><span style="color: #f8f8f2">(</span> <span style="color: #a6e22e">json</span> <span style="color: #f92672">=&gt;</span> <span style="color: #f8f8f2">{</span>
<span style="color: #66d9ef">this</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">$parent</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">score</span> <span style="color: #f92672">=</span> <span style="color: #a6e22e">json</span> <span style="color: #75715e">//将返回的数据赋给app.vue中定义的score</span>
<span style="color: #66d9ef">this</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">$router</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">push</span><span style="color: #f8f8f2">(</span><span style="color: #e6db74">&quot;result&quot;</span><span style="color: #f8f8f2">)</span> <span style="color: #75715e">//然后切换到result页面</span>
<span style="color: #f8f8f2">})</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #f8f8f2">&lt;/</span><span style="color: #f92672">script</span><span style="color: #f8f8f2">&gt;</span>
</pre></div>
</p>
<p>接下来我们就进入查看结果的页面啦,下一篇会详细介绍一下result.vue <br>
ps:想知道button的hover效果是怎么实现的吗?可以戳<a href="http://codepen.io/nw/pen/GqBzJ">Flat &amp; Shiny Button (hover effect)</a>~</p>
</description>
</item>
<item>
<title>hackathon_Match(1)</title>
<link>https://elegenthus.github.io/post/hackathon_Match%281%29/</link>
<pubDate>Wed, 02 Nov 2016 17:07:33 +0800</pubDate>
<guid>https://elegenthus.github.io/post/hackathon_Match%281%29/</guid>
<description><p>上周末和团队的大大们一起去参加了武汉hackathon,真是一次难忘的体验啊~</p>
<hr />
<p>首先奉上我们组的产品<a href="http://match.muxixyz.com">Match</a>,这是一款通过输入两个人的豆瓣、网易云音乐、微博和知乎账号,寻找其共同项,最后输出一个匹配指数的产品,下面我将简单总结一下此次用到的一些前端技术。</p>
<hr />
<p>Match是一个使用vue-router实现页面切换效果的单页web应用,所以我们此次只有一个html页面</p>
<div class="highlight" style="background: #272822"><pre style="line-height: 125%"><span></span><span style="color: #f8f8f2">&lt;</span><span style="color: #f92672">body</span><span style="color: #f8f8f2">&gt;</span>
<span style="color: #f8f8f2">&lt;</span><span style="color: #f92672">div</span> <span style="color: #a6e22e">id</span><span style="color: #f92672">=</span><span style="color: #e6db74">&quot;app&quot;</span><span style="color: #f8f8f2">&gt;&lt;/</span><span style="color: #f92672">div</span><span style="color: #f8f8f2">&gt;</span>
<span style="color: #f8f8f2">&lt;</span><span style="color: #f92672">script</span> <span style="color: #a6e22e">src</span><span style="color: #f92672">=</span><span style="color: #e6db74">&quot;/static/main.js&quot;</span><span style="color: #f8f8f2">&gt;&lt;/</span><span style="color: #f92672">script</span><span style="color: #f8f8f2">&gt;</span>
<span style="color: #f8f8f2">&lt;/</span><span style="color: #f92672">body</span><span style="color: #f8f8f2">&gt;</span>
</pre></div>
<p>重点是main.js部分,它作为入口文件,规定了路由的含义</p>
<div class="highlight" style="background: #272822"><pre style="line-height: 125%"><span></span><span style="color: #66d9ef">import</span> <span style="color: #a6e22e">Vue</span> <span style="color: #a6e22e">from</span> <span style="color: #e6db74">&#39;vue&#39;</span>
<span style="color: #66d9ef">import</span> <span style="color: #a6e22e">Index</span> <span style="color: #a6e22e">from</span> <span style="color: #e6db74">&#39;./componnents/index&#39;</span>
<span style="color: #66d9ef">import</span> <span style="color: #a6e22e">Result</span> <span style="color: #a6e22e">from</span> <span style="color: #e6db74">&#39;./componnents/result&#39;</span>
<span style="color: #66d9ef">import</span> <span style="color: #a6e22e">VueRouter</span> <span style="color: #a6e22e">from</span> <span style="color: #e6db74">&#39;vue-router&#39;</span>
<span style="color: #66d9ef">import</span> <span style="color: #a6e22e">App</span> <span style="color: #a6e22e">from</span> <span style="color: #e6db74">&#39;./componnents/app&#39;</span><span style="color: #f8f8f2">;</span>
<span style="color: #a6e22e">Vue</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">use</span><span style="color: #f8f8f2">(</span><span style="color: #a6e22e">VueRouter</span><span style="color: #f8f8f2">)</span>
<span style="color: #66d9ef">const</span> <span style="color: #a6e22e">router</span> <span style="color: #f92672">=</span> <span style="color: #66d9ef">new</span> <span style="color: #a6e22e">VueRouter</span><span style="color: #f8f8f2">({</span>
<span style="color: #a6e22e">mode</span><span style="color: #f92672">:</span> <span style="color: #e6db74">&#39;history&#39;</span><span style="color: #f8f8f2">,</span>
<span style="color: #a6e22e">base</span><span style="color: #f92672">:</span> <span style="color: #a6e22e">__dirname</span><span style="color: #f8f8f2">,</span>
<span style="color: #a6e22e">routes</span><span style="color: #f92672">:</span> <span style="color: #f8f8f2">[</span>
<span style="color: #f8f8f2">{</span> <span style="color: #a6e22e">path</span><span style="color: #f92672">:</span> <span style="color: #e6db74">&#39;/&#39;</span><span style="color: #f8f8f2">,</span> <span style="color: #a6e22e">component</span><span style="color: #f92672">:</span> <span style="color: #a6e22e">Index</span> <span style="color: #f8f8f2">},</span>
<span style="color: #f8f8f2">{</span> <span style="color: #a6e22e">path</span><span style="color: #f92672">:</span> <span style="color: #e6db74">&#39;/result&#39;</span><span style="color: #f8f8f2">,</span> <span style="color: #a6e22e">component</span><span style="color: #f92672">:</span> <span style="color: #a6e22e">Result</span> <span style="color: #f8f8f2">}</span>
<span style="color: #f8f8f2">]</span>
<span style="color: #f8f8f2">})</span>
<span style="color: #66d9ef">new</span> <span style="color: #a6e22e">Vue</span><span style="color: #f8f8f2">({</span>
<span style="color: #a6e22e">router</span><span style="color: #f8f8f2">,</span>
<span style="color: #a6e22e">el</span><span style="color: #f92672">:</span> <span style="color: #e6db74">&#39;#app&#39;</span><span style="color: #f8f8f2">,</span>
<span style="color: #a6e22e">render</span><span style="color: #f92672">:</span> <span style="color: #a6e22e">h</span> <span style="color: #f92672">=&gt;</span> <span style="color: #a6e22e">h</span><span style="color: #f8f8f2">(</span><span style="color: #a6e22e">App</span><span style="color: #f8f8f2">)</span>
<span style="color: #f8f8f2">})</span>
</pre></div>
<p>先是引入vue-router,然后调用构造器VueRouter定义了一个新的router,在routes中规定访问路径,并将路径path和组件component一一对应地映射起来,然后将其挂载到一个已经vue初始化好的#app元素上<br></p>
<p>我们这次的页面中设置了一个父组件,两个子组件<br>
app.vue为此次的父组件,它这次的主要使命是渲染出背景,match的背景中的星空效果是参考<a href="http://codepen.io/CKH4/pen/vNyyaL">Random Pure CSS Parallax Stars</a> <br>
可是如果将1000多行的css代码直接copy到组件的style标签中,会使该组件页面上的别的样式变得复杂难懂,于是我们在这里将该段css代码放到background.scss中,在用import标签引入。</p>
<p><div class="highlight" style="background: #272822"><pre style="line-height: 125%"><span></span><span style="color: #960050; background-color: #1e0010">@</span><span style="color: #66d9ef">import</span> <span style="color: #e6db74">&quot;../scss/background.scss&quot;</span><span style="color: #f8f8f2">;</span>
</pre></div>
</p>
<p>在父组件中我们除了将vue创建的app放上去之外,不要忘记页面样式的reset哦~由于这次我们主要使用到的是input输入框和button,于是我们对它们的样式进行重置,部分代码如下</p>
<p><div class="highlight" style="background: #272822"><pre style="line-height: 125%"><span></span><span style="color: #f92672">html,</span> <span style="color: #f92672">body</span><span style="color: #f8f8f2">{</span>
<span style="color: #f8f8f2">margin</span><span style="color: #f92672">:</span> <span style="color: #ae81ff">0</span><span style="color: #f8f8f2">;</span>
<span style="color: #f8f8f2">padding</span><span style="color: #f92672">:</span> <span style="color: #ae81ff">0</span><span style="color: #f8f8f2">;</span>
<span style="color: #f8f8f2">width</span><span style="color: #f92672">:</span> <span style="color: #ae81ff">100%</span><span style="color: #f8f8f2">;</span>
<span style="color: #f8f8f2">height</span><span style="color: #f92672">:</span> <span style="color: #ae81ff">100%</span><span style="color: #f8f8f2">;</span>
<span style="color: #f8f8f2">font-family</span><span style="color: #f92672">:</span> <span style="color: #f8f8f2">PingFang</span> <span style="color: #f8f8f2">SC;</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #f92672">input</span><span style="color: #f8f8f2">{</span>
<span style="color: #f8f8f2">border</span><span style="color: #f92672">:</span><span style="color: #ae81ff">0</span><span style="color: #f8f8f2">;</span>
<span style="color: #f8f8f2">outline</span><span style="color: #f92672">:</span> <span style="color: #f8f8f2">none;</span>
<span style="color: #f8f8f2">padding</span><span style="color: #f92672">:</span> <span style="color: #ae81ff">0</span><span style="color: #f8f8f2">;</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #f92672">button</span><span style="color: #f8f8f2">{</span>
<span style="color: #f8f8f2">border</span><span style="color: #f92672">:</span><span style="color: #ae81ff">0</span><span style="color: #f8f8f2">;</span>
<span style="color: #f8f8f2">padding</span><span style="color: #f92672">:</span><span style="color: #ae81ff">0</span><span style="color: #f8f8f2">;</span>
<span style="color: #f8f8f2">box</span><span style="color: #f92672">-</span><span style="color: #f8f8f2">sizing</span><span style="color: #f92672">:</span> <span style="color: #f8f8f2">content</span><span style="color: #f92672">-</span><span style="color: #f8f8f2">box;</span>
<span style="color: #f8f8f2">margin</span><span style="color: #f92672">:</span> <span style="color: #ae81ff">0</span><span style="color: #f8f8f2">;</span>
<span style="color: #f8f8f2">background-color</span><span style="color: #f92672">:</span> <span style="color: #f8f8f2">transparent;</span>
<span style="color: #f8f8f2">outline</span><span style="color: #f92672">:</span> <span style="color: #f8f8f2">none;</span>
<span style="color: #f8f8f2">}</span>
</pre></div>
</p>
<p>然后不要忘记定义一个score对象来储存返回的数据</p>
<p><div class="highlight" style="background: #272822"><pre style="line-height: 125%"><span></span><span style="color: #66d9ef">export</span> <span style="color: #66d9ef">default</span> <span style="color: #f8f8f2">{</span>
<span style="color: #a6e22e">name</span><span style="color: #f92672">:</span> <span style="color: #e6db74">&quot;App&quot;</span><span style="color: #f8f8f2">,</span>
<span style="color: #a6e22e">data</span> <span style="color: #f8f8f2">(){</span>
<span style="color: #66d9ef">return</span> <span style="color: #f8f8f2">{</span>
<span style="color: #a6e22e">score</span><span style="color: #f92672">:</span><span style="color: #f8f8f2">{}</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #f8f8f2">}</span>
</pre></div>
</p>
<p>最后在app.vue的template中除了放背景的相关代码,还要使用<router-view></router-view>标签,它用于渲染匹配的组件。</p>
<p><div class="highlight" style="background: #272822"><pre style="line-height: 125%"><span></span><span style="color: #f8f8f2">&lt;</span><span style="color: #f92672">router-view</span><span style="color: #f8f8f2">&gt;&lt;/</span><span style="color: #f92672">router-view</span><span style="color: #f8f8f2">&gt;</span>
</pre></div>
</p>
<p>以上就是父组件的内容,下次我们将会对两个子组件index.vue和result.vue进行详细介绍</p>
</description>
</item>
<item>
<title>vuetest</title>
<link>https://elegenthus.github.io/post/vuetest/</link>
<pubDate>Sat, 22 Oct 2016 17:17:19 +0800</pubDate>
<guid>https://elegenthus.github.io/post/vuetest/</guid>
<description><p>这是个人的一点小笔记,记录在学习vue的过程中解决的的一些小需求</p>
<hr />
<p>目标:在一个页面的父组件中发请求,请求API中数据分别返回到两个子组件<br><br>
假设我们请求的第一个API是一个包含多个对象的数组,其中每一个对象包含如下评论信息</p>
<div class="highlight" style="background: #272822"><pre style="line-height: 125%"><span></span> <span style="color: #f8f8f2">{</span>
<span style="color: #f92672">&quot;id&quot;</span><span style="color: #f8f8f2">:</span> <span style="color: #ae81ff">1244</span><span style="color: #f8f8f2">,</span>
<span style="color: #f92672">&quot;likes&quot;</span><span style="color: #f8f8f2">:</span> <span style="color: #ae81ff">0</span> <span style="color: #f8f8f2">,</span>
<span style="color: #f92672">&quot;liked&quot;</span><span style="color: #f8f8f2">:</span> <span style="color: #66d9ef">false</span><span style="color: #f8f8f2">,</span>
<span style="color: #f92672">&quot;user_name&quot;</span><span style="color: #f8f8f2">:</span> <span style="color: #e6db74">&quot;学而2016&quot;</span><span style="color: #f8f8f2">,</span>
<span style="color: #f92672">&quot;body&quot;</span><span style="color: #f8f8f2">:</span> <span style="color: #e6db74">&quot;比较轻松,期末小组做ppt&quot;</span>
<span style="color: #f8f8f2">}</span>
</pre></div>
<p>我们将从这个API中获取每一个对象的body信息并将其显示在页面上<br><br>
1.首先,我们创建一个part1.vue,这是第一个子组件的页面,在每一个vue组件的页面中,应该有template标签,里面包含页面上显示的内容,有style标签,控制页面的具体样式,有script标签,包含我们对页面上内容的操作。由于我们此次请求信息返回的是一个数组对象,所以我决定在数组中将其以列表的形式展示出来,在template中代码如下</p>
<div class="highlight" style="background: #272822"><pre style="line-height: 125%"><span></span><span style="color: #f8f8f2">&lt;</span><span style="color: #f92672">template</span><span style="color: #f8f8f2">&gt;</span>
<span style="color: #f8f8f2">&lt;</span><span style="color: #f92672">ul</span> <span style="color: #a6e22e">id</span><span style="color: #f92672">=</span><span style="color: #e6db74">&quot;app1&quot;</span><span style="color: #f8f8f2">&gt;</span>
<span style="color: #f8f8f2">&lt;</span><span style="color: #f92672">li</span> <span style="color: #a6e22e">v-for </span><span style="color: #f92672">=</span> <span style="color: #e6db74">&quot;item in list&quot;</span> <span style="color: #f8f8f2">&gt;</span>
{{ item.body }}
<span style="color: #f8f8f2">&lt;/</span><span style="color: #f92672">li</span><span style="color: #f8f8f2">&gt;</span>
<span style="color: #f8f8f2">&lt;/</span><span style="color: #f92672">ul</span><span style="color: #f8f8f2">&gt;</span>
<span style="color: #f8f8f2">&lt;/</span><span style="color: #f92672">template</span><span style="color: #f8f8f2">&gt;</span>
</pre></div>
<p>对了,还没有解释,其中的item是我为返回的数组取的名字,这一步是在在script标签中进行的</p>
<p><div class="highlight" style="background: #272822"><pre style="line-height: 125%"><span></span><span style="color: #f92672">&lt;</span><span style="color: #a6e22e">script</span><span style="color: #f92672">&gt;</span>
<span style="color: #66d9ef">export</span> <span style="color: #66d9ef">default</span> <span style="color: #f8f8f2">{</span>
<span style="color: #a6e22e">data</span><span style="color: #f8f8f2">()</span> <span style="color: #f8f8f2">{</span>
<span style="color: #66d9ef">return</span> <span style="color: #f8f8f2">{</span>
<span style="color: #a6e22e">list</span><span style="color: #f92672">:</span><span style="color: #f8f8f2">[]</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #f92672">&lt;</span><span style="color: #960050; background-color: #1e0010">/script&gt;</span>
</pre></div>
</p>
<p>关于页面的具体样式这里不做赘述,可自行设定,需要提醒的一点是,若用sass来写的话,应该在style标签内加入lang=&lsquo;sass&rsquo;属性<br><br>
2.接下来是第二个子组件part2.vue,其格式和part1.vue差不多,不过这个组件里我只想显示API返回的数组中第一个对象中的&rdquo;user_name&rdquo;和&rdquo;id&rdquo;属性,在此设第一个对象为obj,想重点提醒一下的是这两个属性在显示时要被包含在一个div中,因为一个组件中只能有一个并行的div,template中的代码如下</p>
<p><div class="highlight" style="background: #272822"><pre style="line-height: 125%"><span></span><span style="color: #f8f8f2">&lt;</span><span style="color: #f92672">template</span><span style="color: #f8f8f2">&gt;</span>
<span style="color: #f8f8f2">&lt;</span><span style="color: #f92672">div</span><span style="color: #f8f8f2">&gt;</span>
<span style="color: #f8f8f2">&lt;</span><span style="color: #f92672">div</span><span style="color: #f8f8f2">&gt;</span>
{{ obj.user_name }}
<span style="color: #f8f8f2">&lt;/</span><span style="color: #f92672">div</span><span style="color: #f8f8f2">&gt;</span>
<span style="color: #f8f8f2">&lt;</span><span style="color: #f92672">div</span><span style="color: #f8f8f2">&gt;</span>
{{ obj.id }}
<span style="color: #f8f8f2">&lt;/</span><span style="color: #f92672">div</span><span style="color: #f8f8f2">&gt;</span>
<span style="color: #f8f8f2">&lt;/</span><span style="color: #f92672">div</span><span style="color: #f8f8f2">&gt;</span>
<span style="color: #f8f8f2">&lt;/</span><span style="color: #f92672">template</span><span style="color: #f8f8f2">&gt;</span>
</pre></div>
</p>
<p>3.好了,接下来就是我们的重头戏父组件了,parent.vue <br>
首先上代码</p>
<p><div class="highlight" style="background: #272822"><pre style="line-height: 125%"><span></span><span style="color: #f8f8f2">&lt;</span><span style="color: #f92672">template</span><span style="color: #f8f8f2">&gt;</span>
<span style="color: #f8f8f2">&lt;</span><span style="color: #f92672">div</span><span style="color: #f8f8f2">&gt;</span>
<span style="color: #f8f8f2">&lt;</span><span style="color: #f92672">div</span> <span style="color: #a6e22e">v-show</span><span style="color: #f92672">=</span><span style="color: #e6db74">&quot;isLoading&quot;</span><span style="color: #f8f8f2">&gt;</span>
加载中
<span style="color: #f8f8f2">&lt;/</span><span style="color: #f92672">div</span><span style="color: #f8f8f2">&gt;</span>
<span style="color: #f8f8f2">&lt;</span><span style="color: #f92672">courseInfo</span> <span style="color: #a6e22e">ref</span><span style="color: #f92672">=</span><span style="color: #e6db74">&quot;courseInfo&quot;</span><span style="color: #f8f8f2">&gt;&lt;/</span><span style="color: #f92672">courseInfo</span><span style="color: #f8f8f2">&gt;</span>
<span style="color: #f8f8f2">&lt;</span><span style="color: #f92672">courseComment</span> <span style="color: #a6e22e">ref</span><span style="color: #f92672">=</span><span style="color: #e6db74">&quot;comment&quot;</span><span style="color: #f8f8f2">&gt;&lt;/</span><span style="color: #f92672">courseComment</span><span style="color: #f8f8f2">&gt;</span>
<span style="color: #f8f8f2">&lt;/</span><span style="color: #f92672">div</span><span style="color: #f8f8f2">&gt;</span>
<span style="color: #f8f8f2">&lt;/</span><span style="color: #f92672">template</span><span style="color: #f8f8f2">&gt;</span>
</pre></div>
</p>
<p><div class="highlight" style="background: #272822"><pre style="line-height: 125%"><span></span><span style="color: #f92672">&lt;</span><span style="color: #a6e22e">script</span><span style="color: #f92672">&gt;</span>
<span style="color: #66d9ef">import</span> <span style="color: #a6e22e">part1</span> <span style="color: #a6e22e">from</span> <span style="color: #e6db74">&#39;./part1&#39;</span><span style="color: #75715e">//把子组件和fecth方法import进来</span>
<span style="color: #66d9ef">import</span> <span style="color: #a6e22e">part2</span> <span style="color: #a6e22e">from</span> <span style="color: #e6db74">&#39;./part2&#39;</span>
<span style="color: #66d9ef">import</span> <span style="color: #e6db74">&#39;whatwg-fetch&#39;</span><span style="color: #f8f8f2">;</span>
<span style="color: #66d9ef">export</span> <span style="color: #66d9ef">default</span> <span style="color: #f8f8f2">{</span>
<span style="color: #a6e22e">data</span><span style="color: #f8f8f2">()</span> <span style="color: #f8f8f2">{</span>
<span style="color: #66d9ef">return</span> <span style="color: #f8f8f2">{</span>
<span style="color: #a6e22e">isLoading</span><span style="color: #f92672">:</span><span style="color: #66d9ef">true</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #f8f8f2">},</span>
<span style="color: #a6e22e">mounted</span> <span style="color: #f8f8f2">()</span> <span style="color: #f8f8f2">{</span>
<span style="color: #66d9ef">let</span> <span style="color: #a6e22e">promise1</span> <span style="color: #f92672">=</span> <span style="color: #a6e22e">fetch</span><span style="color: #f8f8f2">(</span><span style="color: #e6db74">&quot;请求的API的相对路径&quot;</span><span style="color: #f8f8f2">).</span><span style="color: #a6e22e">then</span><span style="color: #f8f8f2">(</span> <span style="color: #f8f8f2">(</span><span style="color: #a6e22e">res</span><span style="color: #f8f8f2">)</span> <span style="color: #f92672">=&gt;</span> <span style="color: #f8f8f2">{</span>
<span style="color: #66d9ef">return</span> <span style="color: #a6e22e">res</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">json</span><span style="color: #f8f8f2">()</span>
<span style="color: #f8f8f2">})</span>
<span style="color: #66d9ef">let</span> <span style="color: #a6e22e">promise2</span> <span style="color: #f92672">=</span> <span style="color: #a6e22e">fetch</span><span style="color: #f8f8f2">(</span><span style="color: #e6db74">&quot;请求的API的相对路径&quot;</span><span style="color: #f8f8f2">)</span>
<span style="color: #f8f8f2">.</span><span style="color: #a6e22e">then</span><span style="color: #f8f8f2">(</span> <span style="color: #f8f8f2">(</span><span style="color: #a6e22e">res</span><span style="color: #f8f8f2">)</span> <span style="color: #f92672">=&gt;</span> <span style="color: #f8f8f2">{</span>
<span style="color: #66d9ef">return</span> <span style="color: #a6e22e">res</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">json</span><span style="color: #f8f8f2">()</span>
<span style="color: #f8f8f2">})</span>
<span style="color: #f8f8f2">Promise.</span><span style="color: #a6e22e">all</span><span style="color: #f8f8f2">([</span><span style="color: #a6e22e">promise1</span><span style="color: #f8f8f2">,</span> <span style="color: #a6e22e">promise2</span><span style="color: #f8f8f2">]).</span><span style="color: #a6e22e">then</span><span style="color: #f8f8f2">(</span> <span style="color: #a6e22e">values</span> <span style="color: #f92672">=&gt;</span> <span style="color: #f8f8f2">{</span>
<span style="color: #75715e">//console.log(this.$refs.courseInfo.obj)</span>
<span style="color: #66d9ef">this</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">$refs</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">courseInfo</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">list</span><span style="color: #f92672">=</span> <span style="color: #a6e22e">values</span><span style="color: #f8f8f2">[</span><span style="color: #ae81ff">1</span><span style="color: #f8f8f2">]</span>
<span style="color: #66d9ef">this</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">$refs</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">comment</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">obj</span> <span style="color: #f92672">=</span> <span style="color: #a6e22e">values</span><span style="color: #f8f8f2">[</span><span style="color: #ae81ff">0</span><span style="color: #f8f8f2">]</span>
<span style="color: #66d9ef">this</span><span style="color: #f8f8f2">.</span><span style="color: #a6e22e">isLoading</span> <span style="color: #f92672">=</span> <span style="color: #66d9ef">false</span>
<span style="color: #f8f8f2">})</span>
<span style="color: #f8f8f2">},</span>
<span style="color: #a6e22e">components</span><span style="color: #f92672">:</span><span style="color: #f8f8f2">{</span>
<span style="color: #e6db74">&quot;courseInfo&quot;</span><span style="color: #f92672">:</span><span style="color: #a6e22e">part1</span><span style="color: #f8f8f2">,</span>
<span style="color: #e6db74">&quot;courseComment&quot;</span><span style="color: #f92672">:</span><span style="color: #a6e22e">part2</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #f8f8f2">}</span>
<span style="color: #f92672">&lt;</span><span style="color: #960050; background-color: #1e0010">/script&gt;</span>
</pre></div>
</p>
<p>接下来是几点要注意的地方: <br><br>
a.由于我们这里还会使用fetch方法请求API所以还要先npm装一下fetch(如果只是在最新版的chrome中就不必如此),具体方法见<a href="https://github.com/github/fetch">fetch</a> <br><br>
b.data里面我们定义了一个isloding的值,是为了在浏览器还没有请求到数据之前先显示一个加载中的文字or信息提示一下,这个提示页面会一直存在直到所有的信息请求完毕,然后我们把isloading的值改为false转而显示页面应该显示的信息<br><br>
c.然后开始请求API,由于返回的是数据流,所以用res.json() 把它转换成对象,用promise.all方法对相应的对象进行赋值操作,这里使用了v-ref子组件指定一个索引 ID,这样可以直接访问子组件,详见<a href="https://vuejs.org.cn/guide/components.html">vue组件</a> <br><br>
d.重点强调一下promise.all是等待其参数全部请求成功返回后才会进行下一步的操作,这就正好满足我们希望两个promise成功就拿掉loading页面转而显示请求到的信息这一要求,我们将会在promise.all后的then中将isloading的值改为false <br><br>
e.最后不要忘记了在component中注册一下子组件哦,这样才能在相应的template中用标签形式引入。<br><br></p>
</description>
</item>
</channel>
</rss>