Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
1121 lines (645 loc) · 62.5 KB
/
Copy pathindex.html
File metadata and controls
1121 lines (645 loc) · 62.5 KB
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
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE HTML>
<html lang="zh-hans" >
<head>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>️组件 · Regular官方指南</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="description" content="">
<meta name="generator" content="GitBook 3.2.3">
<meta name="author" content="leeluolee">
<link rel="stylesheet" href="../../gitbook/style.css">
<link rel="stylesheet" href="../../gitbook/gitbook-plugin-splitter/splitter.css">
<link rel="stylesheet" href="../../gitbook/gitbook-plugin-search-plus/search.css">
<link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
<link rel="stylesheet" href="../../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
<link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">
<link rel="next" href="../interpolation.html" />
<link rel="prev" href="../" />
</head>
<body>
<div class="book">
<div class="book-summary">
<div id="book-search-input" role="search">
<input type="text" placeholder="输入并搜索" />
</div>
<nav role="navigation">
<ul class="summary">
<li>
<a href="https://github.com/regularjs/regular" target="_blank" class="custom-link">Github</a>
</li>
<li class="divider"></li>
<li class="chapter " data-level="1.1" data-path="../../">
<a href="../../">
️介绍
</a>
<ul class="articles">
<li class="chapter " data-level="1.1.1" data-path="../../intro/what.html">
<a href="../../intro/what.html">
Regular是什么
</a>
</li>
<li class="chapter " data-level="1.1.2" data-path="../../intro/hello.html">
<a href="../../intro/hello.html">
️你好,Regular
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.2" data-path="../">
<a href="../">
基础
</a>
<ul class="articles">
<li class="chapter active" data-level="1.2.1" data-path="./">
<a href="./">
️组件
</a>
</li>
<li class="chapter " data-level="1.2.2" data-path="../interpolation.html">
<a href="../interpolation.html">
️插值
</a>
</li>
<li class="chapter " data-level="1.2.3" data-path="../event.html">
<a href="../event.html">
️事件
</a>
</li>
<li class="chapter " data-level="1.2.4" data-path="../statement/">
<a href="../statement/">
️语句
</a>
<ul class="articles">
<li class="chapter " data-level="1.2.4.1" data-path="../statement/list.html">
<a href="../statement/list.html">
️列表渲染 - list
</a>
</li>
<li class="chapter " data-level="1.2.4.2" data-path="../statement/if.html">
<a href="../statement/if.html">
️条件渲染 - if
</a>
</li>
<li class="chapter " data-level="1.2.4.3" data-path="../statement/include.html">
<a href="../statement/include.html">
️结构引入 - include
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.2.5" data-path="../../reference/expression.html">
<a href="../../reference/expression.html">
表达式
</a>
</li>
<li class="chapter " data-level="1.2.6" data-path="../data-binding.html">
<a href="../data-binding.html">
数据监听
</a>
</li>
<li class="chapter " data-level="1.2.7" data-path="../filter.html">
<a href="../filter.html">
计算字段与过滤器
</a>
</li>
<li class="chapter " data-level="1.2.8" data-path="../directive.html">
<a href="../directive.html">
️指令
</a>
</li>
<li class="chapter " data-level="1.2.9" data-path="../animation.html">
<a href="../animation.html">
️动画
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.3" data-path="../../advanced/">
<a href="../../advanced/">
深入组件
</a>
<ul class="articles">
<li class="chapter " data-level="1.3.1" data-path="../../advanced/modular.html">
<a href="../../advanced/modular.html">
继承与模块化
</a>
</li>
<li class="chapter " data-level="1.3.2" data-path="../../advanced/composite.html">
<a href="../../advanced/composite.html">
️组合 - 结构复用
</a>
</li>
<li class="chapter " data-level="1.3.3" data-path="../../advanced/modify.html">
<a href="../../advanced/modify.html">
️动态注入
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4" data-path="../../topic/">
<a href="../../topic/">
专题
</a>
<ul class="articles">
<li class="chapter " data-level="1.4.1" data-path="../../topic/template.html">
<a href="../../topic/template.html">
模板管理
</a>
<ul class="articles">
<li class="chapter " data-level="1.4.1.1" >
<a target="_blank" href="https://github.com/regularjs/regular-loader">
单文件组件 - regular-loader
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4.2" >
<a target="_blank" href="https://regularjs.github.io/regular-state/">
️单页系统
</a>
</li>
<li class="chapter " data-level="1.4.3" data-path="../../topic/ssr.html">
<a href="../../topic/ssr.html">
服务端渲染
</a>
</li>
<li class="chapter " data-level="1.4.4" data-path="../../topic/mpregular.html">
<a href="../../topic/mpregular.html">
微信小程序
</a>
</li>
<li class="chapter " data-level="1.4.5" data-path="../../topic/performance.html">
<a href="../../topic/performance.html">
性能指引
</a>
</li>
<li class="chapter " data-level="1.4.6" data-path="../../topic/communication.html">
<a href="../../topic/communication.html">
️跨组件通信
</a>
<ul class="articles">
<li class="chapter " data-level="1.4.6.1" data-path="../../topic/redux.md">
<span>
与redux结合
</a>
</li>
<li class="chapter " data-level="1.4.6.2" data-path="../../topic/rx.md">
<span>
与Rx结合(ing...)
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4.7" data-path="../../topic/todomvc.html">
<a href="../../topic/todomvc.html">
50行代码的todomvc
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.5" data-path="../../reference/">
<a href="../../reference/">
索引手册
</a>
<ul class="articles">
<li class="chapter " data-level="1.5.1" data-path="../../reference/api.html">
<a href="../../reference/api.html">
API
</a>
</li>
<li class="chapter " data-level="1.5.2" data-path="../../reference/styleguide.html">
<a href="../../reference/styleguide.html">
风格指南
</a>
</li>
<li class="chapter " data-level="1.5.3" data-path="../../reference/directive.html">
<a href="../../reference/directive.html">
✔内建指令
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.6" data-path="../../faq/">
<a href="../../faq/">
FAQ
</a>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
本书使用 GitBook 发布
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="../.." >️组件</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<div class="search-plus" id="book-search-results">
<div class="search-noresults">
<section class="normal markdown-section">
<div id="anchor-navigation-ex-navbar"><i class="fa fa-anchor"></i><ul><li><span class="title-icon "></span><a href="#组件---component"><b> </b>组件 - component</a></li><ul><li><span class="title-icon "></span><a href="#组件基础"><b>1. </b>组件基础</a></li><ul><li><span class="title-icon "></span><a href="#声明式调用"><b>1.1. </b>声明式调用</a></li><li><span class="title-icon "></span><a href="#全局别名快捷设置---name"><b>1.2. </b>全局别名快捷设置 - name</a></li></ul><li><span class="title-icon "></span><a href="#prop"><b>2. </b>组件的参数传入与绑定 </a></li><ul><li><span class="title-icon "></span><a href="#data"><b>2.1. </b>参数传递细节说明 </a></li><li><span class="title-icon "></span><a href="#default"><b>2.2. </b>禁止在extend中定义缺省data参数 </a></li></ul><li><span class="title-icon "></span><a href="#生命周期钩子"><b>3. </b>生命周期钩子</a></li><ul><li><span class="title-icon "></span><a href="#config-data----在模板编译前调用"><b>3.1. </b>config( data ) - 在模板编译前调用</a></li><li><span class="title-icon "></span><a href="#init---在模板编译后调用"><b>3.2. </b>init - 在模板编译后调用</a></li><li><span class="title-icon "></span><a href="#destroy---在组件销毁前调用"><b>3.3. </b>destroy - 在组件销毁前调用</a></li><li><span class="title-icon "></span><a href="#modifybodycomponent"><b>3.4. </b>modifyBodyComponent</a></li><li><span class="title-icon "></span><a href="#生命周期图示"><b>3.5. </b>生命周期图示</a></li></ul><li><span class="title-icon "></span><a href="#动态组件"><b>4. </b>动态组件</a></li><li><span class="title-icon "></span><a href="#parent"><b>5. </b>父组件 - $parent </a></li><ul><li><span class="title-icon "></span><a href="#isolate"><b>5.1. </b>组件隔离 </a></li></ul><li><span class="title-icon "></span><a href="#组件的isolate属性"><b>6. </b>组件的isolate属性</a></li><li><span class="title-icon "></span><a href="#ref"><b>7. </b>获取子节点和子组件 </a></li><ul><li><span class="title-icon "></span><a href="#常见dom操作的误区"><b>7.1. </b>常见DOM操作的误区</a></li><li><span class="title-icon "></span><a href="#ref-属性"><b>7.2. </b>ref 属性</a></li><li><span class="title-icon "></span><a href="#ref属性是可动态插值"><b>7.3. </b>ref属性是可动态插值</a></li></ul><li><span class="title-icon "></span><a href="#regulardomelement-获取组件的子节点。"><b>8. </b>Regular.dom.element 获取组件的子节点。</a></li><ul><li><span class="title-icon "></span><a href="#何时使用domelement-何时使用-ref"><b>8.1. </b>何时使用dom.element 何时使用 ref?</a></li></ul><li><span class="title-icon "></span><a href="#其它事项"><b>9. </b>其它事项</a></li><ul><li><span class="title-icon "></span><a href="#没有单节点限制"><b>9.1. </b>没有单节点限制</a></li></ul></ul></ul></div><a href="#组件---component" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><h1 id="组件---component"><a name="组件---component" class="anchor-navigation-ex-anchor" href="#组件---component"><i class="fa fa-link" aria-hidden="true"></i></a> 组件 - component</h1>
<p>「组件化」不仅仅是标签化这种表现层面的描述</p>
<blockquote>
<p>在用户端开发领域,组件应该是一种独立的、可复用的交互元素的封装。</p>
</blockquote>
<p>Regular的组件满足以下等式</p>
<blockquote>
<p>组件 = 模板 + 数据 + 业务逻辑</p>
</blockquote>
<p><strong>提示</strong></p>
<ul>
<li>本节会简单涉及到 <a href="../event.html#dom">DOM事件</a></li>
</ul>
<h2 id="组件基础"><a name="组件基础" class="anchor-navigation-ex-anchor" href="#组件基础"><i class="fa fa-link" aria-hidden="true"></i></a>1. 组件基础</h2>
<p>使用 <strong><a href="../../reference/api.html#extend">Component.extend</a></strong> 定义一个可复用的组件</p>
<pre><code class="lang-js">
<span class="hljs-keyword">const</span> Component = Regular.extend( {
template: <span class="hljs-string">`<button on-click={count+=1}> 你点了{count}次按钮 </button>`</span>,
config( data ){
data.count = <span class="hljs-number">0</span>;
}
});
</code></pre>
<p><code>extend</code> 返回的是一个组件构造函数,你可以直接使用 <code>new</code> 关键字进行实例化</p>
<pre><code class="lang-js">
<span class="hljs-keyword">new</span> Component().$inject(<span class="hljs-built_in">document</span>.body)
<span class="hljs-keyword">new</span> Component().$inject(<span class="hljs-built_in">document</span>.body) <span class="hljs-comment">//可以重复使用</span>
</code></pre>
<script async src="//jsfiddle.net/leeluolee/3qt2g0mo/embed/result,js/"></script>
<p><strong>参考</strong></p>
<ul>
<li><strong><a href="../../reference/api.html#inject">api - $inject</a></strong>: 插入组件到指定节点位置,或恢复组件到游离状态(脱离文档)</li>
</ul>
<h3 id="声明式调用"><a name="声明式调用" class="anchor-navigation-ex-anchor" href="#声明式调用"><i class="fa fa-link" aria-hidden="true"></i></a>1.1. 声明式调用</h3>
<p>除了通过<code>new</code>实现命令式初始化,Regular 支持在别的组件模板中使用声明式调用,这是最常见的使用方式。</p>
<pre><code class="lang-js"><span class="hljs-comment">// 注册名为Component的全局组件</span>
Regular.component(<span class="hljs-string">'Component'</span>, Component)
<span class="hljs-keyword">new</span> Regular({
template:<span class="hljs-string">`
<Component ></Component>
<Component/> <!-- 自闭合 -->
`</span>
})
</code></pre>
<p>效果与上例完全一致</p>
<script async src="//jsfiddle.net/leeluolee/nwds9g6c/embed/result,js/"></script>
<p><strong>参考</strong></p>
<ul>
<li><strong><a href="../reference/api.md#component">api - Component.component</a></strong>: 组件注册</li>
</ul>
<h3 id="全局别名快捷设置---name"><a name="全局别名快捷设置---name" class="anchor-navigation-ex-anchor" href="#全局别名快捷设置---name"><i class="fa fa-link" aria-hidden="true"></i></a>1.2. 全局别名快捷设置 - name</h3>
<p>因为上例这种注册到全局的方式比较普遍, <strong>Regular 支持在 extend 时,直接设置别名</strong></p>
<pre><code class="lang-js">Regular.extend({
name: <span class="hljs-string">'Component'</span> <span class="hljs-comment">// 设置全局别名</span>
<span class="hljs-comment">//...</span>
})
</code></pre>
<h2 id="prop"><a name="prop" class="anchor-navigation-ex-anchor" href="#prop"><i class="fa fa-link" aria-hidden="true"></i></a>2. 组件的参数传入与绑定 </h2>
<p><strong>data</strong> 在 Regular 中是个特殊的实例属性,根据 <a href="../../reference/expression.html">索引:表达式</a> 所述</p>
<blockquote>
<p>数据根路径从 component.data 开始,即 {user} 其实获取的是 component.data.user</p>
</blockquote>
<p>实际上在模板里也可以拿到实例上的其它属性,比如 <code>{this.nickName}</code> 拿到的就是实例上的nickName字段,例如</p>
<pre><code class="lang-js">
<span class="hljs-keyword">new</span> Regular({
template:<span class="hljs-string">`
<div>{this.nickName}</div>
<!-- 相当于 this.data.userName -->
<div>{userName}</div>
`</span>,
nickName: <span class="hljs-string">'leeluolee'</span>,
data: {
userName: <span class="hljs-string">'hzzhenghaibo'</span>
}
})
</code></pre>
<p>这样看起来,似乎 <strong>data</strong> 仅仅只是个短写的语法糖而已。 其实不尽然,因为</p>
<blockquote>
<p><strong>只有data上的字段可以通过声明式属性传入</strong></p>
</blockquote>
<pre><code class="lang-html">
<span class="hljs-tag"><<span class="hljs-name">Component</span> <span class="hljs-attr">title</span>=<span class="hljs-string">'hello'</span> /></span>
</code></pre>
<p>就相当于</p>
<pre><code class="lang-js">
<span class="hljs-keyword">new</span> Component({
data: {
title: <span class="hljs-string">'hello'</span>
}
})
</code></pre>
<p>节点属性会自动成为<code>data</code>对象上的字段。</p>
<h3 id="data"><a name="data" class="anchor-navigation-ex-anchor" href="#data"><i class="fa fa-link" aria-hidden="true"></i></a>2.1. 参数传递细节说明 </h3>
<h4 id="传入属性都会成为data的成员"><a name="传入属性都会成为data的成员" class="anchor-navigation-ex-anchor" href="#传入属性都会成为data的成员"><i class="fa fa-link" aria-hidden="true"></i></a>传入属性都会成为data的成员</h4>
<pre><code class="lang-html"> <span class="hljs-tag"><<span class="hljs-name">Component</span> <span class="hljs-attr">total</span>=<span class="hljs-string">100</span> <span class="hljs-attr">title</span>=<span class="hljs-string">'Hello'</span> ></span><span class="hljs-tag"></<span class="hljs-name">Component</span>></span>
</code></pre>
<p>相当于<code>data.total='100'</code>, <code>data.title='Hello'</code></p>
<blockquote>
<p>注意非属性插值,则传入类型都是字符串</p>
</blockquote>
<h4 id="插值创建双向绑定"><a name="插值创建双向绑定" class="anchor-navigation-ex-anchor" href="#插值创建双向绑定"><i class="fa fa-link" aria-hidden="true"></i></a>插值创建双向绑定</h4>
<p>如果存在插值则会双向绑定,如下例的title与外层的<code>title</code>字段就形成了双向绑定</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">Component</span> <span class="hljs-attr">title</span>=<span class="hljs-string">{title}</span> <span class="hljs-attr">show</span>=<span class="hljs-string">{true}</span> /></span>
</code></pre>
<p><strong>例外</strong>:在这里 <code>show={true}</code> 比较特殊,因为表达式是一个静态值,所以不会产生双向绑定。</p>
<h4 id="value-缺省则默认成为boolean值true"><a name="value-缺省则默认成为boolean值true" class="anchor-navigation-ex-anchor" href="#value-缺省则默认成为boolean值true"><i class="fa fa-link" aria-hidden="true"></i></a>value 缺省,则默认成为Boolean值<code>true</code></h4>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">Component</span> <span class="hljs-attr">show</span> ></span><span class="hljs-tag"></<span class="hljs-name">Component</span>></span>
</code></pre>
<p>上例相当于 <code><Component show = {true} /></code></p>
<h4 id="连缀格式转驼峰"><a name="连缀格式转驼峰" class="anchor-navigation-ex-anchor" href="#连缀格式转驼峰"><i class="fa fa-link" aria-hidden="true"></i></a>连缀格式转驼峰</h4>
<pre><code class="lang-html"> <span class="hljs-tag"><<span class="hljs-name">Component</span> <span class="hljs-attr">show-modal</span>=<span class="hljs-string">{true}</span> <span class="hljs-attr">isHide</span>=<span class="hljs-string">{hide}</span>/></span>
</code></pre>
<p>则 <code>show-modal</code> 会转换为 <code>showModal</code>, 但是 <code>isHide</code> 这种驼峰式的写法仍然支持,并不做变换</p>
<h4 id="事件指令"><a name="事件指令" class="anchor-navigation-ex-anchor" href="#事件指令"><i class="fa fa-link" aria-hidden="true"></i></a>事件指令</h4>
<p>满足<code>on-[eventName]</code>的属性会自动转换为<a href="../event.html">组件事件绑定</a></p>
<pre><code class="lang-html"> <span class="hljs-tag"><<span class="hljs-name">Component</span> <span class="hljs-attr">on-nav</span>=<span class="hljs-string">{this.nav($event)}</span>></span><span class="hljs-tag"></<span class="hljs-name">Component</span>></span>
</code></pre>
<h4 id="其它特殊属性"><a name="其它特殊属性" class="anchor-navigation-ex-anchor" href="#其它特殊属性"><i class="fa fa-link" aria-hidden="true"></i></a>其它特殊属性</h4>
<p>特殊属性如<code>ref</code>和<code>isolate</code> 会有特殊的功能。请参考 <a href="#ref">获取内部节点和组件</a> 和 <a href="#isolate">组件隔离</a></p>
<h3 id="default"><a name="default" class="anchor-navigation-ex-anchor" href="#default"><i class="fa fa-link" aria-hidden="true"></i></a>2.2. 禁止在extend中定义缺省data参数 </h3>
<p><strong>⚠️不要在 extend 或 implement 时定义data属性 !!!</strong> ,这会导致所有实例共享数据。</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> Component = Regular.extend({
name: <span class="hljs-string">'Component'</span>,
template: <span class="hljs-string">`
<h2>{title}</h2>
<button on-click={shared.count+=1}>点击shared.count+1: {shared.count}</button>
<button on-click={instance.count+=1}>点击instance.count+1: {instance.count}</button>
`</span>,
data: {
shared:{
count:<span class="hljs-number">1</span> <span class="hljs-comment">// 会影响到所有实例</span>
}
},
config(data){
data.instance = {count: <span class="hljs-number">1</span>} <span class="hljs-comment">// 每个实例独有</span>
}
}) <span class="hljs-comment">// 每个实例独有</span>
</code></pre>
<script async src="//jsfiddle.net/leeluolee/do4m1erf/embed/result,js/"></script>
<p><strong>参考</strong></p>
<ul>
<li><a href="../../reference/styleguide.html#">风格指南:禁止在 extend 中定义缺省 data 参数</a></li>
</ul>
<h2 id="生命周期钩子"><a name="生命周期钩子" class="anchor-navigation-ex-anchor" href="#生命周期钩子"><i class="fa fa-link" aria-hidden="true"></i></a>3. 生命周期钩子</h2>
<p>Regular 提供了几个常用的生命周期钩子,你可以实现指定方法来处理自定义逻辑</p>
<h3 id="config-data----在模板编译前调用"><a name="config-data----在模板编译前调用" class="anchor-navigation-ex-anchor" href="#config-data----在模板编译前调用"><i class="fa fa-link" aria-hidden="true"></i></a>3.1. config( data ) - 在模板编译前调用</h3>
<pre><code class="lang-js">Regular.extend({
config( data ){
data.title = data.title || <span class="hljs-string">'默认标题'</span>
}
})
</code></pre>
<ul>
<li>data: <strong>合并传入参数</strong>后的data对象</li>
</ul>
<p><strong>参考</strong></p>
<ul>
<li><a href="#prop">参数传递</a></li>
</ul>
<h3 id="init---在模板编译后调用"><a name="init---在模板编译后调用" class="anchor-navigation-ex-anchor" href="#init---在模板编译后调用"><i class="fa fa-link" aria-hidden="true"></i></a>3.2. init - 在模板编译后调用</h3>
<p>在这个阶段,已经可以获取到生成的节点,<strong>但是仍然是游离状态的,未插入到实际DOM节点中</strong></p>
<pre><code class="lang-js">Regular.extend({
template: <span class="hljs-string">`<div ref=container>Hello</div>`</span>,
init(){
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>.$refs.container.innerHTML === <span class="hljs-string">'Hello'</span>); <span class="hljs-comment">//true</span>
}
})
</code></pre>
<p><strong>参考</strong></p>
<ul>
<li><a href="#ref">获取子节点和子组件</a></li>
</ul>
<h3 id="destroy---在组件销毁前调用"><a name="destroy---在组件销毁前调用" class="anchor-navigation-ex-anchor" href="#destroy---在组件销毁前调用"><i class="fa fa-link" aria-hidden="true"></i></a>3.3. destroy - 在组件销毁前调用</h3>
<p>销毁函数 , <strong>注意</strong> 如果要自定义回收函数,务必调用父类 destroy(<code>this.supr</code>)来销毁掉一些通用部分(如事件监听,数据监听)</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> Component = Regular.extend({
init(){
<span class="hljs-keyword">this</span>.onScroll = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{};
<span class="hljs-built_in">window</span>.addEventListener(<span class="hljs-string">'scroll'</span>, <span class="hljs-keyword">this</span>.onScroll)
},
destory(){
<span class="hljs-keyword">this</span>.supr(); <span class="hljs-comment">// 千万别忘记</span>
<span class="hljs-built_in">window</span>.removeEventListener(<span class="hljs-string">'scroll'</span>, <span class="hljs-keyword">this</span>.onScroll)
}
})
</code></pre>
<h3 id="modifybodycomponent"><a name="modifybodycomponent" class="anchor-navigation-ex-anchor" href="#modifybodycomponent"><i class="fa fa-link" aria-hidden="true"></i></a>3.4. modifyBodyComponent</h3>
<p>请参考 @TODO</p>
<h3 id="生命周期图示"><a name="生命周期图示" class="anchor-navigation-ex-anchor" href="#生命周期图示"><i class="fa fa-link" aria-hidden="true"></i></a>3.5. 生命周期图示</h3>
<p><strong>实例化时</strong></p>
<p><img src="https://p1.music.126.net/fMaWu1OPgVHz_0aP9Z5SaQ==/109951163409731716.png" alt=""></p>
<p><strong>销毁时</strong>
<img src="https://p1.music.126.net/wMn6NWuOolhxXd1Ekfo-ow==/109951163409729417.png" alt=""></p>
<h2 id="动态组件"><a name="动态组件" class="anchor-navigation-ex-anchor" href="#动态组件"><i class="fa fa-link" aria-hidden="true"></i></a>4. 动态组件</h2>
<p>Regular 提供了 <code>r-component</code> 用于处理动态组件的需求,这个组件使用is属性来决定使用什么组件来渲染</p>
<ul>
<li>is: 组件名</li>
</ul>
<p>如下例,我们预先定义了<code>MarkdownRender</code> 与 <code>HtmlRender</code>两个组件</p>
<pre><code class="lang-js">
Regular.extend({
name: <span class="hljs-string">'MarkdownRender'</span>,
template: <span class="hljs-string">`<div r-html={mdcontent} ></div>`</span>,
config(data){
<span class="hljs-keyword">this</span>.$watch(<span class="hljs-string">'content'</span>, (content)=>{
data.mdcontent = marked(content)
})
}
})
Regular.extend({
template: <span class="hljs-string">`<div r-html={content}</div>`</span>,
name: <span class="hljs-string">'HtmlRender'</span>
})
</code></pre>
<p>使用<code>r-component</code>来动态实例化组件,注意is属性是个动态插值</p>
<pre><code class="lang-js">
<input type=<span class="hljs-string">"radio"</span> value=<span class="hljs-string">'MarkdownRender'</span> r-model={type} /> 使用markdown
<input type=<span class="hljs-string">"radio"</span> value=<span class="hljs-string">'HtmlRender'</span> r-model={type} /> 是否使用mdrender
<r-component is={type} content={content} />
</code></pre>
<script async src="//jsfiddle.net/leeluolee/a50rbLfh/embed/result,js/"></script>
<p><strong>参考</strong></p>
<ul>
<li>其余属性都会成为指定组件的<code>data</code>参数,请参考<a href="#prop">组件参数小节</a></li>
<li><a href="../data-binding.html#watch">数据监听</a></li>
</ul>
<h2 id="parent"><a name="parent" class="anchor-navigation-ex-anchor" href="#parent"><i class="fa fa-link" aria-hidden="true"></i></a>5. 父组件 - $parent </h2>
<p>在 Regular 中,声明式实例化的组件都具有一个<code>this.$parent</code>属性,它指向直接父组件</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> Example = Regular.extend({
name: <span class="hljs-string">'Example'</span>,
init(){
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>.$parent.name) <span class="hljs-comment">// log 'App'</span>
}
})
<span class="hljs-keyword">const</span> App = Regular.extend({
name: <span class="hljs-string">'App'</span>,
template: <span class="hljs-string">`<Example title={title} />`</span>
})
<span class="hljs-keyword">const</span> main = <span class="hljs-keyword">new</span> Regular({
template: <span class="hljs-string">`<App title={title} />`</span>,
data: {
title: <span class="hljs-string">'main title'</span>
}
})
</code></pre>
<p>直接父组件,决定了子组件执行的数据上下文。比如App接受的<code>title</code>实际指的是<code>main.data.title</code>,而<code>Example</code>的title则指向<code>app.data.title</code>(App的实例)。</p>
<h3 id="isolate"><a name="isolate" class="anchor-navigation-ex-anchor" href="#isolate"><i class="fa fa-link" aria-hidden="true"></i></a>5.1. 组件隔离 </h3>
<p>默认情况下,父子组件之间会建立双向绑定</p>
<pre><code class="lang-js"><input r-model={total} type=number/>
<span class="xml"><span class="hljs-tag"><<span class="hljs-name">pager</span> <span class="hljs-attr">total</span>=<span class="hljs-string">{total}</span>></span><span class="hljs-tag"></<span class="hljs-name">pager</span>></span></span>
</code></pre>
<p>即我在上层组件修改一份数据会导致pager也进行了脏检查,而pager发生数据变化也会引起上层组件发生脏检查,这在有些时候不是我们想看到的,也会影响到组件的整体性能。我们或许希望pager与上层组件完全隔离,而完全通过事件来通信。</p>
<h2 id="组件的isolate属性"><a name="组件的isolate属性" class="anchor-navigation-ex-anchor" href="#组件的isolate属性"><i class="fa fa-link" aria-hidden="true"></i></a>6. 组件的isolate属性</h2>
<p><code>isolate</code>可以实现上述要求,例如上例如果修改为:</p>
<pre><code class="lang-js"><input r-model={total} type=number/>
<span class="xml"><span class="hljs-tag"><<span class="hljs-name">pager</span> <span class="hljs-attr">total</span>=<span class="hljs-string">{total}</span> <span class="hljs-attr">isolate</span> <span class="hljs-attr">on-nav</span>=<span class="hljs-string">'nav'</span>></span><span class="hljs-tag"></<span class="hljs-name">pager</span>></span></span>
</code></pre>
<p>那内嵌组件pager与实际就是完全隔离了,完全等同于JS初始化<code>new Pager().$inject('input', 'after')</code>。</p>
<h2 id="ref"><a name="ref" class="anchor-navigation-ex-anchor" href="#ref"><i class="fa fa-link" aria-hidden="true"></i></a>7. 获取子节点和子组件 </h2>
<p>获取到组件内部的子组件和子节点是使用 Regular 的常见需求。</p>
<h3 id="常见dom操作的误区"><a name="常见dom操作的误区" class="anchor-navigation-ex-anchor" href="#常见dom操作的误区"><i class="fa fa-link" aria-hidden="true"></i></a>7.1. 常见DOM操作的误区</h3>
<p><strong>组件本身应该是完全数据驱动,dom操作应该交由指令处理</strong> 在理念上并没有错,但有时直接操作DOM会简单很多。所以经常有人在 init 中写出以下代码(此时内部节点已经产生)</p>
<p><strong>Bad Case</strong></p>
<pre><code class="lang-js">Regular.extend({
template: <span class="hljs-string">`<div id='xxx'></div>`</span>,
init: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
<span class="hljs-keyword">const</span> elem = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'xxx'</span>); 返回<span class="hljs-literal">null</span>
}
})
</code></pre>
<p>这种做法并不会凑效,因为 init 时此组件并没有插入到文档中,游离状态的组件自然无法用<code>document.getElementById</code>获取到它。</p>
<p>但我们可以使用 ref 来解决</p>
<h3 id="ref-属性"><a name="ref-属性" class="anchor-navigation-ex-anchor" href="#ref-属性"><i class="fa fa-link" aria-hidden="true"></i></a>7.2. ref 属性</h3>
<p><strong>ref</strong> 是个特殊的属性,你可以使用它来标记一个<strong>节点或组件</strong>.</p>
<pre><code class="lang-html">
const component = new Regular({
template: "<span class="hljs-tag"><<span class="hljs-name">input</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">input</span>></span> <span class="hljs-tag"><<span class="hljs-name">component</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">component</span>></span><span class="hljs-tag"></<span class="hljs-name">component</span>></span>"
init: function(){
this.$refs.input.value = 'hahaha'
this.$refs.component.show() // 调用子组件的方法
}
})
</code></pre>
<p>如上例所示,在 compile 之后(比如 init 生命周期里),就可以使用<code>this.$refs</code> 来获取到内部子节点或子组件了。</p>
<blockquote>
<p>还是应尽可能使用数据驱动的方式来构建你的UI</p>
</blockquote>
<h3 id="ref属性是可动态插值"><a name="ref属性是可动态插值" class="anchor-navigation-ex-anchor" href="#ref属性是可动态插值"><i class="fa fa-link" aria-hidden="true"></i></a>7.3. ref属性是可动态插值</h3>
<p>ref属性与其它属性一样可以插值,这样在类似循环渲染的场景中会比较有用</p>
<script async src="//jsfiddle.net/leeluolee/tqLew7ou/embed/js,result/"></script>
<h2 id="regulardomelement-获取组件的子节点。"><a name="regulardomelement-获取组件的子节点。" class="anchor-navigation-ex-anchor" href="#regulardomelement-获取组件的子节点。"><i class="fa fa-link" aria-hidden="true"></i></a>8. Regular.dom.element 获取组件的子节点。</h2>
<p>除了ref,Regular还提供了 <a href="../../reference/api.html#dom"><code>dom.element(component, needAll)</code></a> 可用于获取组件内部的节点。</p>
<pre><code class="lang-js">
<span class="hljs-keyword">const</span> dom = Regular.dom;
<span class="hljs-keyword">const</span> component = <span class="hljs-keyword">new</span> Regular({
template: <span class="hljs-string">`<div id='first'></div><p id='last'></p>`</span>
init: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
<span class="hljs-built_in">console</span>.log(dom.element(<span class="hljs-keyword">this</span>)) <span class="hljs-comment">// => div#first </span>
<span class="hljs-built_in">console</span>.log(dom.element(<span class="hljs-keyword">this</span>, <span class="hljs-literal">true</span>)) <span class="hljs-comment">// => [div#first, p#last]</span>
}
})
</code></pre>
<p><strong>说明</strong></p>
<ul>
<li>如果needAll为<code>true</code>,返回子节点数组。</li>
</ul>
<h3 id="何时使用domelement-何时使用-ref"><a name="何时使用domelement-何时使用-ref" class="anchor-navigation-ex-anchor" href="#何时使用domelement-何时使用-ref"><i class="fa fa-link" aria-hidden="true"></i></a>8.1. 何时使用dom.element 何时使用 ref?</h3>
<p><code>ref</code> 的优点是简单直观而且高效,除此之外还可以 <strong>获取组件</strong> 。 而 <code>dom.element</code> 的优势是不需要做主动的标记,可以提供<code>ref</code>无法满足的能力,例如</p>
<pre><code class="lang-js">
<span class="hljs-keyword">const</span> Component = Regular.extend({
template: <span class="hljs-string">'<div ref="container">Component</div>'</span>
init: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
<span class="hljs-keyword">this</span>.$refs.container <span class="hljs-comment">// ...</span>
}