-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
977 lines (684 loc) · 153 KB
/
index.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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
<link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.png">
<link rel="mask-icon" href="/images/safari-pinned-tab.svg" color="#222">
<link rel="stylesheet" href="/css/main.css">
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
<script id="hexo-configurations">
var NexT = window.NexT || {};
var CONFIG = {"hostname":"example.com","root":"/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"flat"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":true,"comments":{"style":"tabs","active":"gitalk","storage":true,"lazyload":false,"nav":null,"activeClass":"gitalk"},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":5,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
</script>
<meta name="description" content="涉猎的主要编程语言以及技术栈为 Html、Css、Javascript、Vue、 React、Nodejs、Php、Mysql等。">
<meta property="og:type" content="website">
<meta property="og:title" content="atao89">
<meta property="og:url" content="http://example.com/index.html">
<meta property="og:site_name" content="atao89">
<meta property="og:description" content="涉猎的主要编程语言以及技术栈为 Html、Css、Javascript、Vue、 React、Nodejs、Php、Mysql等。">
<meta property="og:locale" content="zh_CN">
<meta property="article:author" content="atao89">
<meta property="article:tag" content="Html、Css、Javascript、Vue、 React、Nodejs、Php、Mysql">
<meta name="twitter:card" content="summary">
<link rel="canonical" href="http://example.com/">
<script id="page-configurations">
// https://hexo.io/docs/variables.html
CONFIG.page = {
sidebar: "",
isHome : true,
isPost : false,
lang : 'zh-CN'
};
</script>
<title>atao89</title>
<noscript>
<style>
.use-motion .brand,
.use-motion .menu-item,
.sidebar-inner,
.use-motion .post-block,
.use-motion .pagination,
.use-motion .comments,
.use-motion .post-header,
.use-motion .post-body,
.use-motion .collection-header { opacity: initial; }
.use-motion .site-title,
.use-motion .site-subtitle {
opacity: initial;
top: initial;
}
.use-motion .logo-line-before i { left: initial; }
.use-motion .logo-line-after i { right: initial; }
</style>
</noscript>
<link rel="alternate" href="/atom.xml" title="atao89" type="application/atom+xml">
</head>
<body itemscope itemtype="http://schema.org/WebPage">
<div class="container use-motion">
<div class="headband"></div>
<header class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-brand-container">
<div class="site-nav-toggle">
<div class="toggle" aria-label="切换导航栏">
<span class="toggle-line toggle-line-first"></span>
<span class="toggle-line toggle-line-middle"></span>
<span class="toggle-line toggle-line-last"></span>
</div>
</div>
<div class="site-meta">
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<h1 class="site-title">atao89</h1>
<span class="logo-line-after"><i></i></span>
</a>
<p class="site-subtitle" itemprop="description">一个专注前端技术的菜鸟</p>
</div>
<div class="site-nav-right">
<div class="toggle popup-trigger">
<i class="fa fa-search fa-fw fa-lg"></i>
</div>
</div>
</div>
<nav class="site-nav">
<ul id="menu" class="main-menu menu">
<li class="menu-item menu-item-home">
<a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>
</li>
<li class="menu-item menu-item-tags">
<a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>
</li>
<li class="menu-item menu-item-categories">
<a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>
</li>
<li class="menu-item menu-item-search">
<a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
</a>
</li>
</ul>
</nav>
<div class="search-pop-overlay">
<div class="popup search-popup">
<div class="search-header">
<span class="search-icon">
<i class="fa fa-search"></i>
</span>
<div class="search-input-container">
<input autocomplete="off" autocapitalize="off"
placeholder="搜索..." spellcheck="false"
type="search" class="search-input">
</div>
<span class="popup-btn-close">
<i class="fa fa-times-circle"></i>
</span>
</div>
<div id="search-result">
<div id="no-result">
<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
</div>
</div>
</div>
</div>
</div>
</header>
<div class="back-to-top">
<i class="fa fa-arrow-up"></i>
<span>0%</span>
</div>
<div class="reading-progress-bar"></div>
<a href="https://github.com/atao89/atao89.github.io" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>
<main class="main">
<div class="main-inner">
<div class="content-wrap">
<div class="content index posts-expand">
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://example.com/2021/11/09/directive/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.png">
<meta itemprop="name" content="atao89">
<meta itemprop="description" content="涉猎的主要编程语言以及技术栈为 Html、Css、Javascript、Vue、 React、Nodejs、Php、Mysql等。">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="atao89">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2021/11/09/directive/" class="post-title-link" itemprop="url">Vue自定义指令</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2021-11-09 13:42:53 / 修改时间:13:45:54" itemprop="dateCreated datePublished" datetime="2021-11-09T13:42:53+08:00">2021-11-09</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/%E5%89%8D%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">前端</span></a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="自定义指令"><a href="#自定义指令" class="headerlink" title="自定义指令"></a>自定义指令</h2><p>在某些情况下,Vue 提供给我们的指令是非常不够的,比如有一个输入框需要在点击的时候能弹出日期控件,这个功能 Vue 并不支持,但 Vue 支持我们自定义指令。</p>
<p>自定义指令和定义组件的方式很类式,也是有全局指令和局部指令之分</p>
<h3 id="全局指令"><a href="#全局指令" class="headerlink" title="全局指令"></a>全局指令</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="comment"><!--在这里使用了一个自定义的指令 v-global--></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">value</span>=<span class="string">""</span> <span class="attr">v-global</span> /></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 注册一个全局自定义指令 v-global,指令名称不用加前缀 v-</span></span><br><span class="line"><span class="comment">// 当元素使用了 v-global 这个指令时会触发对应的 function</span></span><br><span class="line"><span class="comment">// 参数 element:使用指令的元素</span></span><br><span class="line">Vue.directive(<span class="string">'global'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">element</span>)</span>{</span><br><span class="line"> <span class="comment">//默认触发钩子函数的 inserted</span></span><br><span class="line"> element.value = <span class="string">"世界和平"</span>;</span><br><span class="line"> element.focus();</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> vm = <span class="keyword">new</span> Vue({</span><br><span class="line"> <span class="attr">el</span>: <span class="string">'#app'</span></span><br><span class="line">})</span><br></pre></td></tr></table></figure>
<h3 id="局部指令"><a href="#局部指令" class="headerlink" title="局部指令"></a>局部指令</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="comment"><!--在这里使用了一个自定义的指令 v-private--></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">value</span>=<span class="string">""</span> <span class="attr">v-private</span> /></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> vm = <span class="keyword">new</span> Vue({</span><br><span class="line"> <span class="attr">el</span>: <span class="string">'#app'</span>,</span><br><span class="line"> <span class="attr">directives</span>: {</span><br><span class="line"> <span class="comment">//注册一个局布指令 private,指令名称不用加前缀 v-</span></span><br><span class="line"> <span class="comment">// 当元素使用了 v-private 这个指令时会触发对应的 function</span></span><br><span class="line"> <span class="comment">// 参数 element:使用指令的元素</span></span><br><span class="line"> <span class="attr">private</span>: <span class="function"><span class="keyword">function</span>(<span class="params">element</span>)</span>{</span><br><span class="line"> element.style.background = <span class="string">'#ccc'</span>;</span><br><span class="line"> element.value = <span class="string">"世界和平"</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure>
<h3 id="钩子函数"><a href="#钩子函数" class="headerlink" title="钩子函数"></a>钩子函数</h3><p>钩子函数可以理解成是指令的生命周期<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="comment"><!--在这里使用了一个自定义的指令 v-global--></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">v-model</span>=<span class="string">"text"</span> <span class="attr">v-demo</span>=<span class="string">"{color:'red'}"</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">Vue.directive(<span class="string">'demo'</span>, {</span><br><span class="line"> <span class="comment">//被绑定元素插入父节点时调用</span></span><br><span class="line"> <span class="comment">//默认触发此方法 Vue.directive('demo', function(element){})</span></span><br><span class="line"> <span class="comment">//后于 bind 触发 </span></span><br><span class="line"> <span class="comment">//参数 element: 使用指令的元素</span></span><br><span class="line"> <span class="comment">//参数 binding: 使用指令的属性对象</span></span><br><span class="line"> <span class="comment">//参数 vnode: 整个 Vue 实例</span></span><br><span class="line"> <span class="attr">inserted</span>: <span class="function"><span class="keyword">function</span>(<span class="params">element, binding, vnode</span>)</span>{</span><br><span class="line"> <span class="built_in">console</span>.log(<span class="string">'inserted'</span>);</span><br><span class="line"> },</span><br><span class="line"> <span class="comment">//只调用一次,指令第一次绑定到元素时调用,</span></span><br><span class="line"> <span class="comment">//用这个钩子函数可以定义一个在绑定时执行一次的初始化动作</span></span><br><span class="line"> <span class="comment">//先于 inserted 触发</span></span><br><span class="line"> <span class="attr">bind</span>: <span class="function"><span class="keyword">function</span>(<span class="params">element, binding, vnode</span>)</span>{</span><br><span class="line"> <span class="built_in">console</span>.log(<span class="string">'bind'</span>);</span><br><span class="line"> element.style.color = binding.value.color</span><br><span class="line"> },</span><br><span class="line"> <span class="comment">//被绑定元素所在的模板更新时调用,而不论绑定值是否变化</span></span><br><span class="line"> <span class="attr">update</span>: <span class="function"><span class="keyword">function</span>(<span class="params">element, binding, vnode</span>)</span>{</span><br><span class="line"> <span class="built_in">console</span>.log(<span class="string">'update'</span>);</span><br><span class="line"> },</span><br><span class="line"> <span class="comment">//被绑定元素所在模板完成一次更新周期时调用。</span></span><br><span class="line"> <span class="attr">componentUpdated</span>: <span class="function"><span class="keyword">function</span>(<span class="params">element, binding, vnode</span>)</span>{</span><br><span class="line"> <span class="built_in">console</span>.log(<span class="string">'componentUpdated'</span>);</span><br><span class="line"> }</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> vm = <span class="keyword">new</span> Vue({</span><br><span class="line"> <span class="attr">el</span>: <span class="string">'#app'</span>,</span><br><span class="line"> <span class="attr">data</span>:{</span><br><span class="line"> <span class="attr">text</span>: <span class="string">'钩子函数'</span></span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure></p>
<h3 id="案例:自定义日期控件"><a href="#案例:自定义日期控件" class="headerlink" title="案例:自定义日期控件"></a>案例:自定义日期控件</h3><p>这里用 boostrap 的 datepicker 插件实现<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="comment"><!--直接在 jQuery 环境下使用 datepicker 插件--></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">id</span>=<span class="string">"datepicker"</span> <span class="attr">data-date-format</span>=<span class="string">"yyyy-mm-dd"</span>/></span></span><br><span class="line"> <span class="comment"><!--使用 Vue 自定义指令 v-datepicker--></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">v-datepicker</span> <span class="attr">data-date-format</span>=<span class="string">"yyyy-mm-dd"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"保存"</span> @<span class="attr">click</span>=<span class="string">"save"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">span</span>></span>{{dataform.birthday}}<span class="tag"></<span class="name">span</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//在没有使用 Vue 前,datepicker 插件在 jQuery 的环境下是这样使用</span></span><br><span class="line">$(<span class="string">'#datepicker'</span>).datepicker();</span><br><span class="line"></span><br><span class="line"><span class="comment">//使用 Vue 自定义指令 v-datepicker</span></span><br><span class="line">Vue.directive(<span class="string">'datepicker'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">element, binding, vnode</span>)</span>{</span><br><span class="line"> <span class="comment">// data = dataform.birthday</span></span><br><span class="line"> $(element).datepicker({</span><br><span class="line"> <span class="attr">language</span>: <span class="string">'zh-CN'</span>,</span><br><span class="line"> <span class="attr">pickTime</span>: <span class="literal">false</span>,</span><br><span class="line"> <span class="attr">todayBtn</span>: <span class="literal">true</span>,</span><br><span class="line"> <span class="attr">autoclose</span>: <span class="literal">true</span></span><br><span class="line"> }).on(<span class="string">'changeDate'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="comment">//由于不是手动在 input 输入值,所以双向绑定 v-model 无效</span></span><br><span class="line"> <span class="comment">//所以需要手动改变实例的数据模型</span></span><br><span class="line"> <span class="keyword">var</span> data = $(element).data(<span class="string">'model'</span>);</span><br><span class="line"> <span class="keyword">if</span>(data){</span><br><span class="line"> <span class="comment">// datas = ['dataform', 'birthday']</span></span><br><span class="line"> <span class="keyword">var</span> datas = data.split(<span class="string">'.'</span>);</span><br><span class="line"> <span class="comment">//context = vm</span></span><br><span class="line"> <span class="keyword">var</span> context = vnode.context;</span><br><span class="line"> <span class="comment">//循环属性自动添加</span></span><br><span class="line"> datas.map(<span class="function">(<span class="params">ele, idx</span>) =></span> {</span><br><span class="line"> <span class="comment">//最后一个属性就直接赋值</span></span><br><span class="line"> <span class="keyword">if</span>(idx == datas.length - <span class="number">1</span>){</span><br><span class="line"> context[ele] = element.value</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="comment">//动态添加属性</span></span><br><span class="line"> context = context[ele]</span><br><span class="line"> }</span><br><span class="line"> })</span><br><span class="line"> }</span><br><span class="line"> })</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> vm = <span class="keyword">new</span> Vue({</span><br><span class="line"> <span class="attr">el</span>: <span class="string">'#app'</span>,</span><br><span class="line"> <span class="attr">data</span>: {</span><br><span class="line"> <span class="attr">dataform</span>: {}</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">methods</span>: {</span><br><span class="line"> <span class="attr">save</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="comment">//使用 $set 更新 dataform</span></span><br><span class="line"> <span class="comment">//更多 $set 的使用在下面继续介绍</span></span><br><span class="line"> <span class="built_in">this</span>.$set(<span class="built_in">this</span>.dataform)</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure></p>
<h2 id="set-介绍"><a href="#set-介绍" class="headerlink" title="$set 介绍"></a>$set 介绍</h2><p>当实例对象 data 先设置好了结构,比如:data: {dataform: {}},在后期想添加一个属性 username 时,这个 username 不会自动绑定到视图当中,所以调用 $set(原对象,新属性名,属性值) 进行绑定到视图当中<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"set"</span> @<span class="attr">click</span>=<span class="string">"set"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">span</span>></span>{{dataform.username}}<span class="tag"></<span class="name">span</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> vm = <span class="keyword">new</span> Vue({</span><br><span class="line"> <span class="attr">el</span>: <span class="string">'#app'</span>,</span><br><span class="line"> <span class="attr">data</span>: {</span><br><span class="line"> <span class="attr">dataform</span>: {}</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">methods</span>: {</span><br><span class="line"> <span class="attr">set</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="comment">// 直接赋值不会显示在视图上</span></span><br><span class="line"> <span class="comment">// this.dataform.username = '123'</span></span><br><span class="line"> <span class="comment">//改用 $set 更新可以在视图上显示</span></span><br><span class="line"> <span class="built_in">this</span>.$set(<span class="built_in">this</span>.dataform, <span class="string">'username'</span>, <span class="string">'123'</span>)</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure></p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://example.com/2021/11/08/router/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.png">
<meta itemprop="name" content="atao89">
<meta itemprop="description" content="涉猎的主要编程语言以及技术栈为 Html、Css、Javascript、Vue、 React、Nodejs、Php、Mysql等。">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="atao89">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2021/11/08/router/" class="post-title-link" itemprop="url">路由</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2021-11-08 16:43:12 / 修改时间:16:46:01" itemprop="dateCreated datePublished" datetime="2021-11-08T16:43:12+08:00">2021-11-08</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/%E5%89%8D%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">前端</span></a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="路由"><a href="#路由" class="headerlink" title="路由"></a>路由</h2><p>通过 URL 映射到对应的功能实现,Vue 的路由使用要先引入 vue-router.js</p>
<h3 id="基本路由入门"><a href="#基本路由入门" class="headerlink" title="基本路由入门"></a>基本路由入门</h3><p>定义 component<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> Foo = { <span class="attr">template</span>: <span class="string">'<div><h1>Foo Content</h1></div>'</span> };</span><br><span class="line"><span class="keyword">const</span> Bar = { <span class="attr">template</span>: <span class="string">'<div><h1>Bar Content</h1></div>'</span> };</span><br></pre></td></tr></table></figure><br>定义路由规则<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//每个路由应该映射一个组件。 其中"component" 可以是自定义的组件</span></span><br><span class="line"><span class="comment">//当 url 为 http://localhost/index.html#/foo 页面会渲染组件 Foo</span></span><br><span class="line"><span class="comment">//当 url 为 http://localhost/index.html#/bar 页面会渲染组件 Bar</span></span><br><span class="line"><span class="keyword">const</span> routes = [</span><br><span class="line"> {<span class="attr">path</span>: <span class="string">'/foo'</span>, <span class="attr">component</span>: Foo},</span><br><span class="line"> {<span class="attr">path</span>: <span class="string">'/bar'</span>, <span class="attr">component</span>: Bar}</span><br><span class="line">]</span><br></pre></td></tr></table></figure><br>使用<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Hello VueRouter<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span></span><br><span class="line"><span class="comment"><!-- 使用 router-link 组件来导航. --></span></span><br><span class="line"><span class="comment"><!-- 通过传入 `to` 属性指定链接. --></span></span><br><span class="line"><span class="comment"><!-- <router-link> 默认会被渲染成一个 `<a>` 标签 --></span></span><br><span class="line"> <span class="comment"><!-- 属性 `to` 对应生成 `<a>` 标签的 `href` 属性--></span></span><br><span class="line"> <span class="tag"><<span class="name">router-link</span> <span class="attr">to</span>=<span class="string">"/foo"</span>></span>Foo<span class="tag"></<span class="name">router-link</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">router-link</span> <span class="attr">to</span>=<span class="string">"/bar"</span>></span>Bar<span class="tag"></<span class="name">router-link</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">p</span>></span></span><br><span class="line"> <span class="comment"><!--路由匹配的组件在此处渲染--></span></span><br><span class="line"> <span class="tag"><<span class="name">router-view</span>></span><span class="tag"></<span class="name">router-view</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> router = <span class="keyword">new</span> VueRouter({</span><br><span class="line"> routes <span class="comment">// (缩写)相当于 routes: routes</span></span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> Vue({</span><br><span class="line"> router</span><br><span class="line">}).$mount(<span class="string">'#app'</span>)</span><br></pre></td></tr></table></figure><br><a target="_blank" rel="noopener" href="https://dk-lan.github.io/vue/VueBasic/Router/router.html">效果预览</a></p>
<h3 id="路由参数"><a href="#路由参数" class="headerlink" title="路由参数"></a>路由参数</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Hello VueRouter<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">router-link</span> <span class="attr">to</span>=<span class="string">"/user/1"</span>></span>User1<span class="tag"></<span class="name">router-link</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">router-link</span> <span class="attr">to</span>=<span class="string">"/user/2"</span>></span>User2<span class="tag"></<span class="name">router-link</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">router-view</span>></span><span class="tag"></<span class="name">router-view</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure>
<p>通过对象 $route.params 来获取参数<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> User = { <span class="attr">template</span>: <span class="string">'<div><h1>{{$route.params.userid}}</h1></div>'</span> };</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> routes = [</span><br><span class="line"> {<span class="attr">path</span>: <span class="string">'/user/:userid'</span>, <span class="attr">component</span>: User}</span><br><span class="line">] </span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> router = <span class="keyword">new</span> VueRouter({</span><br><span class="line"> routes</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> Vue({</span><br><span class="line"> router</span><br><span class="line">}).$mount(<span class="string">'#app'</span>)</span><br></pre></td></tr></table></figure><br><a target="_blank" rel="noopener" href="https://dk-lan.github.io/vue/VueBasic/Router/params.html">效果预览</a></p>
<h3 id="嵌套路由"><a href="#嵌套路由" class="headerlink" title="嵌套路由"></a>嵌套路由</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Hello VueRouter<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">router-link</span> <span class="attr">to</span>=<span class="string">"/floor1"</span>></span>一楼<span class="tag"></<span class="name">router-link</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">router-view</span>></span><span class="tag"></<span class="name">router-view</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> Floor1 = { </span><br><span class="line"> <span class="attr">template</span>: <span class="string">`</span></span><br><span class="line"><span class="string"> <div></span></span><br><span class="line"><span class="string"> <h1>一楼</h1></span></span><br><span class="line"><span class="string"> <router-link to="/floor1/floor2">二楼</router-link></span></span><br><span class="line"><span class="string"> <router-view></router-view></span></span><br><span class="line"><span class="string"> </div>`</span> </span><br><span class="line">};</span><br><span class="line"><span class="keyword">const</span> Floor2 = { <span class="attr">template</span>: <span class="string">'<div><h1>二楼</h1></div>'</span> }; </span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> routes = [</span><br><span class="line"> {</span><br><span class="line"> <span class="attr">path</span>: <span class="string">'/floor1'</span>,</span><br><span class="line"> <span class="attr">component</span>: Floor1,</span><br><span class="line"> <span class="attr">children</span>: [{</span><br><span class="line"> <span class="comment">// floor2 会被渲染在 Floor1 的 <router-view> 中</span></span><br><span class="line"> <span class="attr">path</span>: <span class="string">'floor2'</span>,</span><br><span class="line"> <span class="attr">component</span>: Floor2</span><br><span class="line"> }]</span><br><span class="line"> }</span><br><span class="line">] </span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> router = <span class="keyword">new</span> VueRouter({</span><br><span class="line"> routes</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> Vue({</span><br><span class="line"> router</span><br><span class="line">}).$mount(<span class="string">'#app'</span>)</span><br></pre></td></tr></table></figure>
<p><a target="_blank" rel="noopener" href="https://dk-lan.github.io/vue/VueBasic/Router/routerChildren.html">效果预览</a></p>
<h2 id="编程式导航"><a href="#编程式导航" class="headerlink" title="编程式导航"></a>编程式导航</h2><p>用 javascript 跳转路由<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Hello VueRouter<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span></span><br><span class="line"> <span class="comment"><!--用 `router-link` 组件进行跳转--></span></span><br><span class="line"> <span class="tag"><<span class="name">router-link</span> <span class="attr">to</span>=<span class="string">"/floor1"</span>></span>一楼<span class="tag"></<span class="name">router-link</span>></span></span><br><span class="line"> <span class="comment"><!--编程式导航1:router.replace--></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"一楼"</span> @<span class="attr">click</span>=<span class="string">"router.replace('/floor1')"</span>></span></span><br><span class="line"> <span class="comment"><!--编程式导航2:router.push()--></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"一楼"</span> @<span class="attr">click</span>=<span class="string">"router.push('/floor1')"</span>></span></span><br><span class="line"> <span class="comment"><!--编程式导航3:router.push({})--></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"一楼"</span> @<span class="attr">click</span>=<span class="string">"router.push({path: '/floor1'})"</span>></span> </span><br><span class="line"> <span class="comment"><!--编程式导航4:router.push({}) 传参数 #/floor1/123--></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"一楼"</span> @<span class="attr">click</span>=<span class="string">"router.push({path: '/floor1', params: {id: 123}})"</span>></span> </span><br><span class="line"> <span class="comment"><!--编程式导航5:router.push({}) 传参数 #/floor1?id=123--></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"一楼"</span> @<span class="attr">click</span>=<span class="string">"router.push({path: '/floor1', query: {id: 123}})"</span>></span> </span><br><span class="line"> <span class="tag"></<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">router-view</span>></span><span class="tag"></<span class="name">router-view</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure></p>
<h2 id="命名路由"><a href="#命名路由" class="headerlink" title="命名路由"></a>命名路由</h2><p>在路由映射表中添加属性 name,用以对该路由映射规则命名,在编程式导航跳转路由时可以用 router.push({name: ‘名称’})<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Hello VueRouter<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">router-link</span> <span class="attr">to</span>=<span class="string">"/floor1"</span>></span>一楼<span class="tag"></<span class="name">router-link</span>></span></span><br><span class="line"> <span class="comment"><!--编程式导航4:router.push({name: '名称'})--></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"一楼"</span> @<span class="attr">click</span>=<span class="string">"router.push({name: 'floor1'})"</span>></span> </span><br><span class="line"> <span class="tag"></<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">router-view</span>></span><span class="tag"></<span class="name">router-view</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> Floor1 = { </span><br><span class="line"> <span class="attr">template</span>: <span class="string">`</span></span><br><span class="line"><span class="string"> <div></span></span><br><span class="line"><span class="string"> <h1>一楼</h1></span></span><br><span class="line"><span class="string"> <router-link to="/floor1/floor2">二楼</router-link></span></span><br><span class="line"><span class="string"> <router-view></router-view></span></span><br><span class="line"><span class="string"> </div>`</span> </span><br><span class="line">};</span><br><span class="line"><span class="keyword">const</span> Floor2 = { <span class="attr">template</span>: <span class="string">'<div><h1>二楼</h1></div>'</span> }; </span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> routes = [</span><br><span class="line"> {</span><br><span class="line"> <span class="attr">path</span>: <span class="string">'/floor1'</span>,</span><br><span class="line"> <span class="attr">component</span>: Floor1,</span><br><span class="line"> <span class="attr">name</span>: <span class="string">'floor1'</span>, <span class="comment">//命名</span></span><br><span class="line"> <span class="attr">children</span>: [{</span><br><span class="line"> <span class="comment">// floor2 会被渲染在 Floor1 的 <router-view> 中</span></span><br><span class="line"> <span class="attr">path</span>: <span class="string">'floor2'</span>,</span><br><span class="line"> <span class="attr">component</span>: Floor2,</span><br><span class="line"> <span class="attr">name</span>: <span class="string">'floor2'</span> <span class="comment">//命名</span></span><br><span class="line"> }]</span><br><span class="line"> }</span><br><span class="line">] </span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> router = <span class="keyword">new</span> VueRouter({</span><br><span class="line"> routes</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> Vue({</span><br><span class="line"> router</span><br><span class="line">}).$mount(<span class="string">'#app'</span>)</span><br></pre></td></tr></table></figure></p>
<h2 id="命名视图"><a href="#命名视图" class="headerlink" title="命名视图"></a>命名视图</h2><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Hello VueRouter<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">router-view</span>></span><span class="tag"></<span class="name">router-view</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">router-view</span> <span class="attr">name</span>=<span class="string">"a"</span>></span><span class="tag"></<span class="name">router-view</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">router-view</span> <span class="attr">name</span>=<span class="string">"b"</span>></span><span class="tag"></<span class="name">router-view</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">p</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> router = <span class="keyword">new</span> VueRouter({</span><br><span class="line"> <span class="attr">routes</span>: [</span><br><span class="line"> {</span><br><span class="line"> <span class="attr">path</span>: <span class="string">'/'</span>,</span><br><span class="line"> <span class="attr">components</span>: {</span><br><span class="line"> <span class="attr">default</span>: {</span><br><span class="line"> <span class="attr">template</span>: <span class="string">'<h1>defalut router view</h1>'</span></span><br><span class="line"> },</span><br><span class="line"> <span class="attr">a</span>: {</span><br><span class="line"> <span class="attr">template</span>: <span class="string">'<h1>a router view</h1>'</span></span><br><span class="line"> },</span><br><span class="line"> <span class="attr">b</span>: {</span><br><span class="line"> <span class="attr">template</span>: <span class="string">'<h1>b router view</h1>'</span></span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> ]</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> Vue({</span><br><span class="line"> <span class="attr">el</span>: <span class="string">'#app'</span>,</span><br><span class="line"> router</span><br><span class="line">})</span><br></pre></td></tr></table></figure>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://example.com/2021/11/08/vuex/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.png">
<meta itemprop="name" content="atao89">
<meta itemprop="description" content="涉猎的主要编程语言以及技术栈为 Html、Css、Javascript、Vue、 React、Nodejs、Php、Mysql等。">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="atao89">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2021/11/08/vuex/" class="post-title-link" itemprop="url">Vuex</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2021-11-08 16:30:18 / 修改时间:16:35:04" itemprop="dateCreated datePublished" datetime="2021-11-08T16:30:18+08:00">2021-11-08</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/%E5%89%8D%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">前端</span></a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="Vuex"><a href="#Vuex" class="headerlink" title="Vuex"></a>Vuex</h2><p>在介绍 Vuex 之前,我先抛出一个问题,如下<br>主要描述的结构是:</p>
<ul>
<li>组件A<ul>
<li>组件B<ul>
<li>组件B1<ul>
<li>事件:Event1</li>
<li>事件:Event2</li>
<li>事件:Event3</li>
</ul>
</li>
<li>组件B2<ul>
<li>事件:Event1</li>
<li>事件:Event2</li>
<li>事件:Event3</li>
</ul>
</li>
</ul>
</li>
<li>组件C<ul>
<li>组件C1<ul>
<li>事件:Event1</li>
<li>事件:Event2</li>
<li>事件:Event3</li>
</ul>
</li>
<li>组件C2<ul>
<li>事件:Event1</li>
<li>事件:Event2</li>
<li>事件:Event3</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>现在的问题要解决是组件C1或者C2要和组件B1或B2通信,前面有详细讲过 Vue 本身组件之间通信的方法,最简单的就是 <code>$parent</code> 和 <code>$children</code>,如果不知道如何使用的请回到上面看 <a target="_blank" rel="noopener" href="https://github.com/dk-lan/vue-erp/tree/master/VueBasic/Communication">组件通信</a></p>
<p>现在用 <code>$parent</code> 和 <code>$children</code> 来上面的问题:组件C1调用组件B2的方法<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">this</span>.$parent.$parent.$children[<span class="number">0</span>].$children[<span class="number">1</span>].Event1()</span><br></pre></td></tr></table></figure><br>代码写到这问题解决了,但是如果再多几层的组件嵌套关系,又或者组件层级关系因需求发生了改变,那上面的代码又得重新改变。</p>
<p>为了解决这样的问题,有人想到了把为何不把所有组件的方法统一到一个公共对象,因为对象是一个无序属性集合,而且还是引用类型,然后把这个对象放到最外层,这样不管组件嵌套多少级,都能调用到对应的方法。</p>
<p>那上面的案例就可以生成如下面这种结构的对象了<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = {</span><br><span class="line"> <span class="attr">A</span>: {},</span><br><span class="line"> <span class="attr">B</span>: {},</span><br><span class="line"> <span class="attr">B1</span>: {</span><br><span class="line"> <span class="attr">Event1</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{},</span><br><span class="line"> <span class="attr">Event2</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{},</span><br><span class="line"> <span class="attr">Event3</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{}</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">B2</span>: {</span><br><span class="line"> <span class="attr">Event1</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{},</span><br><span class="line"> <span class="attr">Event2</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{},</span><br><span class="line"> <span class="attr">Event3</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{}</span><br><span class="line"> }, </span><br><span class="line"> <span class="attr">C</span>: {},</span><br><span class="line"> <span class="attr">C1</span>: {</span><br><span class="line"> <span class="attr">Event1</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{},</span><br><span class="line"> <span class="attr">Event2</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{},</span><br><span class="line"> <span class="attr">Event3</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{}</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">C2</span>: {</span><br><span class="line"> <span class="attr">Event1</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{},</span><br><span class="line"> <span class="attr">Event2</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{},</span><br><span class="line"> <span class="attr">Event3</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{}</span><br><span class="line"> }, </span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<h3 id="跨组件通信之——公共对象"><a href="#跨组件通信之——公共对象" class="headerlink" title="跨组件通信之——公共对象"></a>跨组件通信之——公共对象</h3><p>场景:在组件 home 里面使用组件 counter,在 home 组件中实现计数,在组件 counter 同步显示当前计数。</p>
<h4 id="common-js"><a href="#common-js" class="headerlink" title="common.js"></a>common.js</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> {</span><br><span class="line"> <span class="attr">state</span>: {</span><br><span class="line"> <span class="attr">count</span>: <span class="number">0</span></span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h4 id="countercomponent"><a href="#countercomponent" class="headerlink" title="countercomponent"></a>countercomponent</h4><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">template</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h3</span>></span>count-{{common.state.count}}<span class="tag"></<span class="name">h3</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"><span class="tag"></<span class="name">template</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">script</span>></span><span class="javascript"></span></span><br><span class="line"><span class="javascript"> <span class="keyword">import</span> common <span class="keyword">from</span> <span class="string">'../../common/common.js'</span></span></span><br><span class="line"><span class="javascript"> <span class="keyword">export</span> <span class="keyword">default</span> {</span></span><br><span class="line"><span class="javascript"> <span class="function"><span class="title">data</span>(<span class="params"></span>)</span>{</span></span><br><span class="line"><span class="javascript"> <span class="keyword">return</span> {</span></span><br><span class="line"><span class="javascript"> common</span></span><br><span class="line"><span class="javascript"> }</span></span><br><span class="line"><span class="javascript"> }</span></span><br><span class="line"><span class="javascript"> }</span></span><br><span class="line"><span class="javascript"></span><span class="tag"></<span class="name">script</span>></span></span><br></pre></td></tr></table></figure>
<h4 id="homecomponent"><a href="#homecomponent" class="headerlink" title="homecomponent"></a>homecomponent</h4><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">template</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">fieldset</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">legend</span>></span><span class="tag"><<span class="name">h3</span>></span>home<span class="tag"></<span class="name">h3</span>></span><span class="tag"></<span class="name">legend</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"increment"</span> @<span class="attr">click</span>=<span class="string">"increment"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">span</span>></span>{{common.state.count}}<span class="tag"></<span class="name">span</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">fieldset</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">fieldset</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">legend</span>></span><span class="tag"><<span class="name">h3</span>></span>counter<span class="tag"></<span class="name">h3</span>></span><span class="tag"></<span class="name">legend</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">counter</span>></span><span class="tag"></<span class="name">counter</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">fieldset</span>></span></span><br><span class="line"> </span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"><span class="tag"></<span class="name">template</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">script</span>></span><span class="javascript"></span></span><br><span class="line"><span class="javascript"> <span class="keyword">import</span> common <span class="keyword">from</span> <span class="string">'../../common/common.js'</span></span></span><br><span class="line"><span class="javascript"> <span class="keyword">import</span> counter <span class="keyword">from</span> <span class="string">'../counter/counter.vue'</span></span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript"> <span class="keyword">export</span> <span class="keyword">default</span> {</span></span><br><span class="line"><span class="javascript"> <span class="function"><span class="title">data</span>(<span class="params"></span>)</span>{</span></span><br><span class="line"><span class="javascript"> <span class="keyword">return</span> {</span></span><br><span class="line"><span class="javascript"> common</span></span><br><span class="line"><span class="javascript"> }</span></span><br><span class="line"><span class="javascript"> },</span></span><br><span class="line"><span class="javascript"> <span class="attr">components</span>: {</span></span><br><span class="line"><span class="javascript"> datagrid,</span></span><br><span class="line"><span class="javascript"> counter</span></span><br><span class="line"><span class="javascript"> },</span></span><br><span class="line"><span class="javascript"> <span class="attr">methods</span>: {</span></span><br><span class="line"><span class="javascript"> <span class="function"><span class="title">increment</span>(<span class="params"></span>)</span>{</span></span><br><span class="line"><span class="javascript"> <span class="built_in">this</span>.common.state.count += <span class="number">1</span>;</span></span><br><span class="line"><span class="javascript"> }</span></span><br><span class="line"><span class="javascript"> }</span></span><br><span class="line"><span class="javascript"> }</span></span><br><span class="line"><span class="javascript"></span><span class="tag"></<span class="name">script</span>></span></span><br></pre></td></tr></table></figure>
<p>上面的案例是通过公共对象解决了简单的跨组件通信的问题,但如果组件 counter 也想能可以实现计数,那最简单的实现方法就是在组件 counter 当中也写一个事件 increment,但这样一来,事件就重复了。</p>
<p>封装实现就是把事件 increment 也放到 common.js 对象中去,然后组件都调用这个公共的方法。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> state = {</span><br><span class="line"> <span class="attr">count</span>: <span class="number">0</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> mutations = {</span><br><span class="line"> <span class="function"><span class="title">increment</span>(<span class="params"></span>)</span>{</span><br><span class="line"> state.count += <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> {</span><br><span class="line"> state,</span><br><span class="line"> mutations</span><br><span class="line">};</span><br></pre></td></tr></table></figure></p>
<p>看似解决了问题,但是如果场景更复杂,组件层级更深,组件间要通信的信息更多,组件间存在同名属性同名方法但不同逻辑等,目前这种方法就解决不了,需要进行更深层次的封装。</p>
<p>Vuex 就是上面这种解决方案的更深层次的实现。</p>
<h3 id="跨组件通信之——Vuex"><a href="#跨组件通信之——Vuex" class="headerlink" title="跨组件通信之——Vuex"></a>跨组件通信之——Vuex</h3><h4 id="store-js"><a href="#store-js" class="headerlink" title="store.js"></a>store.js</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">'vue'</span></span><br><span class="line"><span class="keyword">import</span> Vuex <span class="keyword">from</span> <span class="string">'vuex'</span></span><br><span class="line"></span><br><span class="line">Vue.use(Vuex)</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> state = {</span><br><span class="line"> <span class="attr">count</span>: <span class="number">0</span></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> mutations = {</span><br><span class="line"> <span class="function"><span class="title">increment</span>(<span class="params"></span>)</span>{</span><br><span class="line"> state.count += <span class="number">1</span>;</span><br><span class="line"> },</span><br><span class="line"> <span class="function"><span class="title">decrement</span>(<span class="params"></span>)</span>{</span><br><span class="line"> state.count -= <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> store = <span class="keyword">new</span> Vuex.Store({</span><br><span class="line"> state,</span><br><span class="line"> mutations</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> store</span><br></pre></td></tr></table></figure>
<h4 id="app-js"><a href="#app-js" class="headerlink" title="app.js"></a>app.js</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">'vue'</span></span><br><span class="line"><span class="keyword">import</span> store <span class="keyword">from</span> <span class="string">'./vuex/store'</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> homeComponent <span class="keyword">from</span> <span class="string">'./components/home/home.vue'</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> Vue({</span><br><span class="line"> <span class="attr">el</span>: <span class="string">'#app'</span>,</span><br><span class="line"> store,</span><br><span class="line"> <span class="attr">render</span>:<span class="function"><span class="params">h</span> =></span> h(homeComponent)</span><br><span class="line">})</span><br></pre></td></tr></table></figure>
<h4 id="homecomponent-1"><a href="#homecomponent-1" class="headerlink" title="homecomponent"></a>homecomponent</h4><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"increment"</span> @<span class="attr">click</span>=<span class="string">"increment"</span>/></span></span><br><span class="line"><span class="tag"><<span class="name">span</span>></span>{{$store.state.count}}<span class="tag"></<span class="name">span</span>></span></span><br><span class="line"><span class="tag"><<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>></span><span class="javascript"></span></span><br><span class="line"><span class="javascript"> <span class="attr">methods</span>: {</span></span><br><span class="line"><span class="javascript"> <span class="function"><span class="title">increment</span>(<span class="params"></span>)</span>{</span></span><br><span class="line"><span class="javascript"> <span class="built_in">this</span>.$store.commit(<span class="string">'increment'</span>);</span></span><br><span class="line"><span class="javascript"> }</span></span><br><span class="line"><span class="javascript"> } </span></span><br><span class="line"><span class="javascript"></span><span class="tag"></<span class="name">script</span>></span></span><br></pre></td></tr></table></figure>
<h4 id="countercomponent-1"><a href="#countercomponent-1" class="headerlink" title="countercomponent"></a>countercomponent</h4><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">span</span>></span>{{$store.state.count}}<span class="tag"></<span class="name">span</span>></span></span><br></pre></td></tr></table></figure>
<h3 id="小结"><a href="#小结" class="headerlink" title="小结"></a>小结</h3><ul>
<li>Vuex 实例 store,添加到 Vue 实例化当中去,然后整个 Vue 实例都能通过 $store 获取到公共对象 store。</li>
<li>用 <code>this.$store.commit('increment');</code> 触发 mutations 的 increment 方法</li>
<li>state 是通过 mutations 来改变</li>
<li>用<code>$store.state.count</code>获取 store 的 state。</li>
</ul>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://example.com/2021/11/05/components/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.png">
<meta itemprop="name" content="atao89">
<meta itemprop="description" content="涉猎的主要编程语言以及技术栈为 Html、Css、Javascript、Vue、 React、Nodejs、Php、Mysql等。">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="atao89">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2021/11/05/components/" class="post-title-link" itemprop="url">Vue组件</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2021-11-05 16:25:10 / 修改时间:16:42:25" itemprop="dateCreated datePublished" datetime="2021-11-05T16:25:10+08:00">2021-11-05</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/%E5%89%8D%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">前端</span></a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="Vue组件"><a href="#Vue组件" class="headerlink" title="Vue组件"></a>Vue组件</h2><p>组件(Component)是前端在单页面应用(SPA)上最好的一种实现方式,把所有功能模块拆解成单独的组件,每个组件都有独立的作用域,且还可以相互通信</p>
<h3 id="认识单页面应用(SPA)"><a href="#认识单页面应用(SPA)" class="headerlink" title="认识单页面应用(SPA)"></a>认识单页面应用(SPA)</h3><p>在传统的页面之间跳转,是通过刷新,重新渲染一个页面而实现,在渲染的过程中势必要加载外部资源文件,页面在服务器中渲染出来是通过一系列的生命周期,在这个过程中会因为网速等硬件问题直接影响页面的加载速度,为解决这一问题,前端在新的设计模式上引入了组件的概念,页面之间的跳转变成了组件之间的切换,不需要重新加载整个页面,也不用考虑页面的生命周期,换成组件的生命周期,在性能上大大的提升了。</p>
<h3 id="Vue-的组件实现"><a href="#Vue-的组件实现" class="headerlink" title="Vue 的组件实现"></a>Vue 的组件实现</h3><h4 id="全局组件"><a href="#全局组件" class="headerlink" title="全局组件"></a>全局组件</h4><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="comment"><!--组件的使用--></span></span><br><span class="line"> <span class="tag"><<span class="name">global-component</span>></span><span class="tag"></<span class="name">global-component</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//组件的定义 Vue.component(组件名称, {template})</span></span><br><span class="line">Vue.component(<span class="string">'global-component'</span>, {</span><br><span class="line"> <span class="attr">template</span>: <span class="string">'<h1>全局组件</h1>'</span></span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> vm = <span class="keyword">new</span> Vue({</span><br><span class="line"> <span class="attr">el</span>: <span class="string">'#app'</span></span><br><span class="line">})</span><br></pre></td></tr></table></figure>
<p>最终渲染的效果<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>全局组件<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure></p>
<h4 id="局部组件"><a href="#局部组件" class="headerlink" title="局部组件"></a>局部组件</h4><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="comment"><!--组件的使用--></span></span><br><span class="line"> <span class="tag"><<span class="name">private-component</span>></span><span class="tag"></<span class="name">private-component</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//组件的定义 Vue.component(组件名称, {template})</span></span><br><span class="line"><span class="keyword">var</span> vm = <span class="keyword">new</span> Vue({</span><br><span class="line"> <span class="attr">el</span>: <span class="string">'#app'</span>,</span><br><span class="line"> <span class="attr">components</span>:{</span><br><span class="line"> <span class="string">'private-component'</span>: {</span><br><span class="line"> <span class="attr">template</span>: <span class="string">'<h1>局部组件</h1>'</span></span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure>
<p>最终渲染的效果<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>局部组件<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure></p>
<h3 id="组件是一个单独的作用域"><a href="#组件是一个单独的作用域" class="headerlink" title="组件是一个单独的作用域"></a>组件是一个单独的作用域</h3><p>每个组件都有单独的作用域<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span>{{count}}<span class="tag"></<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">component1</span>/></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span> </span><br></pre></td></tr></table></figure><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> vm = <span class="keyword">new</span> Vue({</span><br><span class="line"> <span class="attr">el</span>: <span class="string">'#app'</span>,</span><br><span class="line"> <span class="attr">data</span>: {</span><br><span class="line"> <span class="attr">count</span>: <span class="number">10</span></span><br><span class="line"> },</span><br><span class="line"> <span class="attr">methods</span>: {</span><br><span class="line"> <span class="attr">increment</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="built_in">this</span>.count += <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">components</span>:{</span><br><span class="line"> <span class="string">'component1'</span>: {</span><br><span class="line"> <span class="attr">template</span>: <span class="string">'<button v-on:click="increment">{{ count }}</button>'</span>,</span><br><span class="line"> <span class="attr">data</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="comment">//在组件里面 data 一定是 function 并返回一个对象</span></span><br><span class="line"> <span class="keyword">return</span> {</span><br><span class="line"> <span class="attr">count</span>: <span class="number">0</span></span><br><span class="line"> }</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">methods</span>: {</span><br><span class="line"> <span class="attr">increment</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="built_in">this</span>.count += <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure><br>渲染结果为<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span>10<span class="tag"></<span class="name">p</span>></span></span><br><span class="line"> <span class="comment"><!--</span></span><br><span class="line"><span class="comment"> 此按钮每次点击都会自增 1,而 p 标签永远都是为 10</span></span><br><span class="line"><span class="comment"> 原因为组件的作用域是单独的</span></span><br><span class="line"><span class="comment"> --></span></span><br><span class="line"> <span class="tag"><<span class="name">button</span>></span>0<span class="tag"></<span class="name">button</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span> </span><br></pre></td></tr></table></figure><br><a target="_blank" rel="noopener" href="https://dk-lan.github.io/vue/VueBasic/Component/component.html">效果预览</a></p>
<h3 id="特殊的-HTML-结构中使用-is"><a href="#特殊的-HTML-结构中使用-is" class="headerlink" title="特殊的 HTML 结构中使用 is"></a>特殊的 HTML 结构中使用 is</h3><p>比如在下拉列表(select)元素里面,子元素必须为 option,则在使用组件的时候用 is<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">select</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">option</span> <span class="attr">is</span>=<span class="string">"privateOption"</span>></span><span class="tag"></<span class="name">option</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">select</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> vm = <span class="keyword">new</span> Vue({</span><br><span class="line"> <span class="attr">el</span>: <span class="string">'#app'</span>,</span><br><span class="line"> <span class="attr">components</span>: {</span><br><span class="line"> <span class="string">'privateOption'</span>: {</span><br><span class="line"> <span class="attr">template</span>: <span class="string">'<option value=1>1</otpion>'</span></span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure><br>渲染结果<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">select</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">option</span> <span class="attr">value</span>=<span class="string">"1"</span>></span>1<span class="tag"></<span class="name">option</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">select</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure></p>
<h3 id="动态组件-is"><a href="#动态组件-is" class="headerlink" title="动态组件 - :is"></a>动态组件 - :is</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span> <span class="attr">style</span>=<span class="string">"display: none;"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"changeLight"</span> @<span class="attr">click</span>=<span class="string">"changeLight"</span> /></span></span><br><span class="line"> <span class="tag"><<span class="name">br</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span> <span class="attr">:is</span>=<span class="string">"show"</span>></span><span class="tag"></<span class="name">p</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>></span><span class="javascript"></span></span><br><span class="line"><span class="javascript"> <span class="keyword">var</span> vm = <span class="keyword">new</span> Vue({</span></span><br><span class="line"><span class="javascript"> <span class="attr">el</span>: <span class="string">'#app'</span>,</span></span><br><span class="line"><span class="javascript"> <span class="attr">data</span>: {</span></span><br><span class="line"><span class="javascript"> <span class="attr">show</span>: <span class="string">'red'</span>,</span></span><br><span class="line"><span class="javascript"> },</span></span><br><span class="line"><span class="javascript"> <span class="attr">methods</span>:{</span></span><br><span class="line"><span class="javascript"> <span class="attr">changeLight</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span></span><br><span class="line"><span class="javascript"> <span class="built_in">this</span>.show = <span class="built_in">this</span>.show == <span class="string">'red'</span> ? <span class="string">'green'</span> : <span class="string">'red'</span>;</span></span><br><span class="line"><span class="javascript"> }</span></span><br><span class="line"><span class="javascript"> },</span></span><br><span class="line"><span class="javascript"> <span class="attr">components</span>: {</span></span><br><span class="line"><span class="javascript"> <span class="attr">red</span>: {</span></span><br><span class="line"><span class="javascript"> <span class="attr">template</span>: <span class="string">'<h1>Red</h1>'</span></span></span><br><span class="line"><span class="javascript"> },</span></span><br><span class="line"><span class="javascript"> <span class="attr">green</span>: {</span></span><br><span class="line"><span class="javascript"> <span class="attr">template</span>: <span class="string">'<h1>Green</h1>'</span></span></span><br><span class="line"><span class="javascript"> }</span></span><br><span class="line"><span class="javascript"> }</span></span><br><span class="line"><span class="javascript"> })</span></span><br><span class="line"><span class="javascript"></span><span class="tag"></<span class="name">script</span>></span></span><br></pre></td></tr></table></figure>
<h3 id="组件属性"><a href="#组件属性" class="headerlink" title="组件属性"></a>组件属性</h3><p>组件的属性要先声明后使用,props: [‘属性名’…]<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="comment"><!--组件的使用--></span></span><br><span class="line"> <span class="tag"><<span class="name">private-component</span> <span class="attr">title</span>=<span class="string">"组件属性"</span> <span class="attr">:text</span>=<span class="string">"mess"</span>></span><span class="tag"></<span class="name">private-component</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//组件的定义 Vue.component(组件名称, {template})</span></span><br><span class="line"><span class="keyword">var</span> vm = <span class="keyword">new</span> Vue({</span><br><span class="line"> <span class="attr">el</span>: <span class="string">'#app'</span>,</span><br><span class="line"> <span class="attr">data</span>: {</span><br><span class="line"> <span class="attr">mess</span>: <span class="string">'-动态属性'</span></span><br><span class="line"> }</span><br><span class="line"> <span class="attr">components</span>:{</span><br><span class="line"> <span class="string">'private-component'</span>: {</span><br><span class="line"> <span class="attr">template</span>: <span class="string">'<h1>{{title + text}}</h1>'</span>,</span><br><span class="line"> <span class="attr">props</span>: [<span class="string">'title'</span>, <span class="string">'text'</span>]</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure><br>最终渲染的效果<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>组件属性-动态属性<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure></p>
<h3 id="组件自定义事件"><a href="#组件自定义事件" class="headerlink" title="组件自定义事件"></a>组件自定义事件</h3><p>和组件属性不一样的在于 <组件名 v-bind:属性名=””>,属性名要在组件中先声明再使用:props: [‘属性名’]<br>自定义事件:<组件名 v-on:自定义事件名=””>,自定义事件名不需要声明,直接用 $emit() 触发<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span>{{total}}<span class="tag"></<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">increment-total</span> <span class="attr">v-on:count</span>=<span class="string">"incrementTotal"</span>></span><span class="tag"></<span class="name">increment-total</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> vm = <span class="keyword">new</span> Vue({</span><br><span class="line"> <span class="attr">el</span>: <span class="string">'#app'</span>,</span><br><span class="line"> <span class="attr">data</span>: {</span><br><span class="line"> <span class="attr">total</span>: <span class="number">0</span></span><br><span class="line"> },</span><br><span class="line"> <span class="attr">methods</span>: {</span><br><span class="line"> <span class="attr">incrementTotal</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="built_in">this</span>.total += <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">components</span>: {</span><br><span class="line"> <span class="string">'incrementTotal'</span>: {</span><br><span class="line"> <span class="attr">template</span>: <span class="string">'<input type="button" @click="incrementTotal" value="Total" />'</span>,</span><br><span class="line"> <span class="attr">data</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="keyword">return</span> {</span><br><span class="line"> <span class="attr">total</span>: <span class="number">0</span></span><br><span class="line"> }</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">methods</span>: {</span><br><span class="line"> <span class="attr">incrementTotal</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="built_in">this</span>.total += <span class="number">1</span>;</span><br><span class="line"> <span class="built_in">this</span>.$emit(<span class="string">'count'</span>)</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure></p>
<h3 id="slot-分发内容"><a href="#slot-分发内容" class="headerlink" title="slot 分发内容"></a>slot 分发内容</h3><p>Vue 组件默认是覆盖渲染,为了解决这一问题,Vue 提出了 slot 分发内容</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">component1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Sam<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Lucy<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">component1</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Vue.component(<span class="string">'component1'</span>, {</span><br><span class="line"> <span class="attr">template</span>: <span class="string">`</span></span><br><span class="line"><span class="string"> <div></span></span><br><span class="line"><span class="string"> <h1>Tom</h1></span></span><br><span class="line"><span class="string"> <slot></slot></span></span><br><span class="line"><span class="string"> </div></span></span><br><span class="line"><span class="string"> `</span></span><br><span class="line">})</span><br></pre></td></tr></table></figure>
<p>最终渲染的效果<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">component1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Tom<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="comment"><!--</span></span><br><span class="line"><span class="comment"> 如果在组件定义的 template 当中没有 <slot></slot>,那么下面两个 h1 标签将不会存在</span></span><br><span class="line"><span class="comment"> 换句话说就是 <slot></slot> = <h1>Sam</h1><h1>Lucy</h1></span></span><br><span class="line"><span class="comment"> <slot></slot>可以放到 <h1>Tom</h1> 上面进行位置调换</span></span><br><span class="line"><span class="comment"> --></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Sam<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Lucy<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">component1</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure></p>
<h4 id="具名-slot"><a href="#具名-slot" class="headerlink" title="具名 slot"></a>具名 slot</h4><p>如果要将组件里面不同的子元素放到不同的地方,那就为子元素加上一个属性 slot=”名称”,然后在组件定义的时候用名称对应位置 <slot name="名称"></slot>,其它没有 slot 属性的子元素将统一分发到 <slot></slot> 里面<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">component1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Sam<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span> <span class="attr">slot</span>=<span class="string">"lucy"</span>></span>Lucy<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">component1</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">Vue.component(<span class="string">'component1'</span>, {</span><br><span class="line"> <span class="attr">template</span>: <span class="string">`</span></span><br><span class="line"><span class="string"> <div></span></span><br><span class="line"><span class="string"> <slot name="lucy"></slot></span></span><br><span class="line"><span class="string"> <h1>Tom</h1></span></span><br><span class="line"><span class="string"> <slot></slot></span></span><br><span class="line"><span class="string"> </div></span></span><br><span class="line"><span class="string"> `</span></span><br><span class="line">})</span><br></pre></td></tr></table></figure><br>最终渲染的效果<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">component1</span>></span></span><br><span class="line"> <span class="comment"><!--<slot name="lucy"></slot> = <h1 slot="lucy">Lucy</h1>--></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Lucy<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Tom<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="comment"><!--其它没有 slot 属性的子元素将全部分发到 <slot></slot> 标签--></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Sam<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">component1</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure></p>
<h3 id="模版写法"><a href="#模版写法" class="headerlink" title="模版写法"></a>模版写法</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">template</span> <span class="attr">id</span>=<span class="string">"component1"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">v-model</span>=<span class="string">"name"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span>{{name}}<span class="tag"></<span class="name">p</span>></span> </span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"><span class="tag"></<span class="name">template</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">component1</span>/></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span> </span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> vm = <span class="keyword">new</span> Vue({</span><br><span class="line"> <span class="attr">el</span>: <span class="string">'#app'</span>,</span><br><span class="line"> <span class="attr">components</span>: {</span><br><span class="line"> <span class="string">'component1'</span>: {</span><br><span class="line"> <span class="attr">template</span>: <span class="string">'#component1'</span>,</span><br><span class="line"> <span class="attr">data</span>: <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span><br><span class="line"> <span class="keyword">return</span> {<span class="attr">name</span>: <span class="string">'Tom'</span>};</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://example.com/2021/11/05/test/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.png">
<meta itemprop="name" content="atao89">
<meta itemprop="description" content="涉猎的主要编程语言以及技术栈为 Html、Css、Javascript、Vue、 React、Nodejs、Php、Mysql等。">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="atao89">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2021/11/05/test/" class="post-title-link" itemprop="url">认识Vue</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2021-11-05 10:51:48 / 修改时间:16:06:31" itemprop="dateCreated datePublished" datetime="2021-11-05T10:51:48+08:00">2021-11-05</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/%E5%89%8D%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">前端</span></a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="认识Vue"><a href="#认识Vue" class="headerlink" title="认识Vue"></a>认识Vue</h2><p>关于 Vue 的描述有不少,不外乎都会拿来与 Angular 和 React 对比,同样头顶 MVVM 双向数据驱动设计模式光环的 Angular 自然被对比的最多,但到目前为止,Angular 在热度上已明显不及 Vue,性能已成为最大的诟病。</p>
<p>Vue、React 和 Angular,其实都是在原生 JS 基础上,对面向对象不一样的实现方式而已,所以要想使用这三者中的任意一种,首先要有一定的 JS 基础和对面向对象有一定的认识。</p>
<p>在代码层面,Vue 只是一个构造函数,整个 Vue 的实现都在实例化这个构造函数开始。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">script</span> <span class="attr">src</span>=<span class="string">"https://unpkg.com/vue/dist/vue.js"</span>></span><span class="tag"></<span class="name">script</span>></span></span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> vm = <span class="keyword">new</span> Vue({</span><br><span class="line"> <span class="attr">el</span>: <span class="string">'#app'</span><span class="comment">// Vue 实例元素</span></span><br><span class="line"> <span class="attr">data</span>: {</span><br><span class="line"> <span class="comment">//数据</span></span><br><span class="line"> }</span><br><span class="line"> ...</span><br><span class="line">})</span><br></pre></td></tr></table></figure>
<h3 id="认识数据驱动模式"><a href="#认识数据驱动模式" class="headerlink" title="认识数据驱动模式"></a>认识数据驱动模式</h3><p>开始接触前端编程的基本上都是先学习 DOM 节点操作,jQuery 在这一领域上一度成为了标准,所以在前端编程的领域中,jQuery 几乎是每个开发者的标配。随着前后端分离的成熟,产品或项目都趋于分布式部署,开发者已不满足于操作 DOM 节点, 设计模式便慢慢的被前端化。</p>
<p>数据驱动的设计模式在思维逻辑上与 DOM 节点操作完全不一样。<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"div1"</span>></span><span class="tag"></<span class="name">div</span>></span></span><br><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">span</span>></span>{{message}}<span class="tag"></<span class="name">span</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//DOM 节点操作</span></span><br><span class="line"><span class="built_in">document</span>.getElementById(<span class="string">'div1'</span>).innerText = <span class="string">'节点被动改变'</span> </span><br><span class="line"></span><br><span class="line"><span class="comment">//Vue 数据驱动: 当 message 发生改变的时候,span 会相应的发生改变,而不需要手动去改变 span。</span></span><br><span class="line"><span class="keyword">var</span> vm = <span class="keyword">new</span> Vue({</span><br><span class="line"> <span class="attr">el</span>: <span class="string">'#app'</span>,</span><br><span class="line"> <span class="attr">data</span>: {</span><br><span class="line"> <span class="attr">message</span>: <span class="string">'我是通过映射显示的文本'</span></span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure></p>
<h3 id="认识-MVVM-模式"><a href="#认识-MVVM-模式" class="headerlink" title="认识 MVVM 模式"></a>认识 MVVM 模式</h3><p>M:Model,称之为数据模型,在前端以对象的形式表现。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> data = {<span class="attr">message</span>: <span class="string">'我就是一个数据模型'</span>}</span><br></pre></td></tr></table></figure><br>V:View,视图,也就是 HTML<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">id</span>=<span class="string">"app"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">span</span>></span>我是视图<span class="tag"></<span class="name">span</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure><br>VM:ViewModel,就是连接数据和视图的桥梁,当 Model 发生改变的时候,ViewModel 便将数据映射到视图。</p>
<p>那么数据驱动模式和 MVVM 模式有什么关系,换句话说,MVVM 是数据驱动模式的一种实现,Vue 是 MVVM 的一种实现。</p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://example.com/2021/11/04/hello-world/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/avatar.png">
<meta itemprop="name" content="atao89">
<meta itemprop="description" content="涉猎的主要编程语言以及技术栈为 Html、Css、Javascript、Vue、 React、Nodejs、Php、Mysql等。">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="atao89">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2021/11/04/hello-world/" class="post-title-link" itemprop="url">Hello Hexo</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2021-11-04 17:39:16" itemprop="dateCreated datePublished" datetime="2021-11-04T17:39:16+08:00">2021-11-04</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar-check"></i>
</span>
<span class="post-meta-item-text">更新于</span>
<time title="修改时间:2021-11-05 16:29:27" itemprop="dateModified" datetime="2021-11-05T16:29:27+08:00">2021-11-05</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/%E5%89%8D%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">前端</span></a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>Welcome to <a target="_blank" rel="noopener" href="https://hexo.io/">Hexo</a>! This is your very first post. Check <a target="_blank" rel="noopener" href="https://hexo.io/docs/">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a target="_blank" rel="noopener" href="https://hexo.io/docs/troubleshooting.html">troubleshooting</a> or you can ask me on <a target="_blank" rel="noopener" href="https://github.com/hexojs/hexo/issues">GitHub</a>.</p>
<h2 id="Quick-Start"><a href="#Quick-Start" class="headerlink" title="Quick Start"></a>Quick Start</h2><h3 id="Create-a-new-post"><a href="#Create-a-new-post" class="headerlink" title="Create a new post"></a>Create a new post</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo new <span class="string">"My New Post"</span></span><br></pre></td></tr></table></figure>
<p>More info: <a target="_blank" rel="noopener" href="https://hexo.io/docs/writing.html">Writing</a></p>
<h3 id="Run-server"><a href="#Run-server" class="headerlink" title="Run server"></a>Run server</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo server</span><br></pre></td></tr></table></figure>
<p>More info: <a target="_blank" rel="noopener" href="https://hexo.io/docs/server.html">Server</a></p>
<h3 id="Generate-static-files"><a href="#Generate-static-files" class="headerlink" title="Generate static files"></a>Generate static files</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo generate</span><br></pre></td></tr></table></figure>
<p>More info: <a target="_blank" rel="noopener" href="https://hexo.io/docs/generating.html">Generating</a></p>
<h3 id="Deploy-to-remote-sites"><a href="#Deploy-to-remote-sites" class="headerlink" title="Deploy to remote sites"></a>Deploy to remote sites</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo deploy</span><br></pre></td></tr></table></figure>
<p>More info: <a target="_blank" rel="noopener" href="https://hexo.io/docs/one-command-deployment.html">Deployment</a></p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
</div>
<script>
window.addEventListener('tabs:register', () => {
let { activeClass } = CONFIG.comments;
if (CONFIG.comments.storage) {
activeClass = localStorage.getItem('comments_active') || activeClass;
}
if (activeClass) {
let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
if (activeTab) {
activeTab.click();
}
}
});
if (CONFIG.comments.storage) {
window.addEventListener('tabs:click', event => {
if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
let commentClass = event.target.classList[1];
localStorage.setItem('comments_active', commentClass);
});
}
</script>
</div>
<div class="toggle sidebar-toggle">
<span class="toggle-line toggle-line-first"></span>
<span class="toggle-line toggle-line-middle"></span>
<span class="toggle-line toggle-line-last"></span>
</div>
<aside class="sidebar">
<div class="sidebar-inner">
<ul class="sidebar-nav motion-element">
<li class="sidebar-nav-toc">
文章目录
</li>
<li class="sidebar-nav-overview">
站点概览
</li>
</ul>
<!--noindex-->
<div class="post-toc-wrap sidebar-panel">
</div>
<!--/noindex-->
<div class="site-overview-wrap sidebar-panel">
<div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
<img class="site-author-image" itemprop="image" alt="atao89"
src="/images/avatar.png">
<p class="site-author-name" itemprop="name">atao89</p>
<div class="site-description" itemprop="description">涉猎的主要编程语言以及技术栈为 Html、Css、Javascript、Vue、 React、Nodejs、Php、Mysql等。</div>
</div>
<div class="site-state-wrap motion-element">
<nav class="site-state">
<div class="site-state-item site-state-posts">
<a href="/archives/">
<span class="site-state-item-count">6</span>
<span class="site-state-item-name">日志</span>
</a>
</div>
<div class="site-state-item site-state-categories">
<a href="/categories/">
<span class="site-state-item-count">1</span>
<span class="site-state-item-name">分类</span></a>
</div>
<div class="site-state-item site-state-tags">
<a href="/tags/">
<span class="site-state-item-count">2</span>
<span class="site-state-item-name">标签</span></a>
</div>
</nav>
</div>
</div>
</div>
</aside>
<div id="sidebar-dimmer"></div>
</div>
</main>
<footer class="footer">
<div class="footer-inner">
<div class="copyright">
©
<span itemprop="copyrightYear">2021</span>
<span class="with-love">
<i class="fa fa-heart"></i>
</span>
<span class="author" itemprop="copyrightHolder">atao89</span>
</div>
<div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
</div>
</div>
</footer>
</div>
<script src="/lib/anime.min.js"></script>
<script src="/lib/pjax/pjax.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/pangu@4/dist/browser/pangu.min.js"></script>
<script src="/lib/velocity/velocity.min.js"></script>
<script src="/lib/velocity/velocity.ui.min.js"></script>
<script src="/js/utils.js"></script>
<script src="/js/motion.js"></script>
<script src="/js/schemes/pisces.js"></script>
<script src="/js/next-boot.js"></script>
<script>
var pjax = new Pjax({
selectors: [
'head title',
'#page-configurations',
'.content-wrap',
'.post-toc-wrap',
'.languages',
'#pjax'
],
switches: {
'.post-toc-wrap': Pjax.switches.innerHTML
},
analytics: false,
cacheBust: false,
scrollTo : !CONFIG.bookmark.enable
});
window.addEventListener('pjax:success', () => {
document.querySelectorAll('script[data-pjax], script#page-configurations, #pjax script').forEach(element => {
var code = element.text || element.textContent || element.innerHTML || '';
var parent = element.parentNode;
parent.removeChild(element);
var script = document.createElement('script');
if (element.id) {
script.id = element.id;
}
if (element.className) {
script.className = element.className;
}
if (element.type) {
script.type = element.type;
}
if (element.src) {
script.src = element.src;
// Force synchronous loading of peripheral JS.
script.async = false;
}
if (element.dataset.pjax !== undefined) {
script.dataset.pjax = '';
}
if (code !== '') {
script.appendChild(document.createTextNode(code));
}
parent.appendChild(script);
});
NexT.boot.refresh();
// Define Motion Sequence & Bootstrap Motion.
if (CONFIG.motion.enable) {
NexT.motion.integrator
.init()
.add(NexT.motion.middleWares.subMenu)
.add(NexT.motion.middleWares.postList)
.bootstrap();
}
NexT.utils.updateSidebarPosition();
});
</script>
<script data-pjax>
(function(){
var canonicalURL, curProtocol;
//Get the <link> tag
var x=document.getElementsByTagName("link");
//Find the last canonical URL
if(x.length > 0){
for (i=0;i<x.length;i++){
if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
canonicalURL=x[i].href;
}
}
}
//Get protocol
if (!canonicalURL){
curProtocol = window.location.protocol.split(':')[0];
}
else{
curProtocol = canonicalURL.split(':')[0];
}
//Get current URL if the canonical URL does not exist
if (!canonicalURL) canonicalURL = window.location.href;
//Assign script content. Replace current URL with the canonical URL
!function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
</script>
<script src="/js/local-search.js"></script>
<div id="pjax">
</div>
</body>
</html>