Repository navigation
Expand file tree
/
Copy pathevent.html
More file actions
910 lines (441 loc) · 38.3 KB
/
Copy pathevent.html
File metadata and controls
910 lines (441 loc) · 38.3 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
<!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="statement/" />
<link rel="prev" href="interpolation.html" />
</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 " data-level="1.2.1" data-path="component/">
<a href="component/">
️组件
</a>
</li>
<li class="chapter " data-level="1.2.2" data-path="interpolation.html">
<a href="interpolation.html">
️插值
</a>
</li>
<li class="chapter active" 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="#事件"><b> </b>事件</a></li><ul><li><span class="title-icon "></span><a href="#dom"><b>1. </b>DOM 事件 </a></li><ul><li><span class="title-icon "></span><a href="#基础dom事件"><b>1.1. </b>基础DOM事件</a></li><li><span class="title-icon "></span><a href="#custom-event"><b>1.2. </b>自定义DOM事件 </a></li><li><span class="title-icon "></span><a href="#事件代理支持"><b>1.3. </b>事件代理支持</a></li><li><span class="title-icon "></span><a href="#event"><b>1.4. </b>$event 对象 </a></li></ul><li><span class="title-icon "></span><a href="#组件事件"><b>2. </b>组件事件</a></li><ul><li><span class="title-icon "></span><a href="#模板里声明组件事件绑定"><b>2.1. </b>模板里声明组件事件绑定</a></li><li><span class="title-icon "></span><a href="#内建生命周期内的组件事件"><b>2.2. </b>内建生命周期内的组件事件</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="#事件绑定语法"><b>3.1. </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="#完整的事件绑定流程"><b>5. </b>完整的事件绑定流程</a></li></ul></ul></div><a href="#事件" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><h1 id="事件"><a name="事件" class="anchor-navigation-ex-anchor" href="#事件"><i class="fa fa-link" aria-hidden="true"></i></a> 事件</h1>
<p>Regular 中事件分为两类</p>
<ul>
<li>DOM事件</li>
<li>组件事件</li>
</ul>
<p>在大部分情况下,它们的使用方式和表现一致。</p>
<h2 id="dom"><a name="dom" class="anchor-navigation-ex-anchor" href="#dom"><i class="fa fa-link" aria-hidden="true"></i></a>1. DOM 事件 </h2>
<blockquote>
<p><strong>DOM节点上</strong> 所有以 <code>on-</code> 开头的属性都会被当做DOM事件处理</p>
</blockquote>
<h3 id="基础dom事件"><a name="基础dom事件" class="anchor-navigation-ex-anchor" href="#基础dom事件"><i class="fa fa-link" aria-hidden="true"></i></a>1.1. 基础DOM事件</h3>
<p><strong>Syntax</strong> : <code>on-event={Expression}</code></p>
<p>每次event触发时,表达式 <code>Expression</code> 都会被运行一次</p>
<p><strong>Example</strong>:</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">button</span> <span class="hljs-attr">on-click</span>=<span class="hljs-string">{count</span> = <span class="hljs-attr">count</span> + <span class="hljs-attr">1</span>}></span> count+1 <span class="hljs-tag"></<span class="hljs-name">button</span>></span> <span class="hljs-tag"><<span class="hljs-name">b</span>></span>{count}<span class="hljs-tag"></<span class="hljs-name">b</span>></span>,
</code></pre>
<script async src="//jsfiddle.net/leeluolee/qktenw5c/embed/js,result/"></script>
<blockquote>
<p><strong>如果表达式执行后的值为false(===),事件的默认行为将会被阻止</strong>,当然你也可以通过<a href="#event">$event.preventDefault()</a>来阻止事件的默认行为。</p>
</blockquote>
<h3 id="custom-event"><a name="custom-event" class="anchor-navigation-ex-anchor" href="#custom-event"><i class="fa fa-link" aria-hidden="true"></i></a>1.2. 自定义DOM事件 </h3>
<p>比如<code>on-tap</code>, <code>on-hold</code></p>
<p><strong>Usage</strong></p>
<p><code>Component.event(event, fn)</code></p>
<p><strong>Arguments</strong></p>
<ul>
<li>event: 自定义事件名</li>
<li>fn(elem, fire)<ul>
<li>elem: 被绑定节点</li>
<li>fire: 触发事件函数</li>
</ul>
</li>
</ul>
<p>注意如果需要做 <strong>销毁工作</strong> ,与指令一样,你需要在 fn 中返回一个函数用来做销毁工作</p>
<p><strong>Example</strong></p>
<p>定义 <code>on-enter</code> 事件处理回车逻辑</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> dom = Regular.dom;
Regular.event(<span class="hljs-string">'enter'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">elem, fire</span>)</span>{
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">update</span>(<span class="hljs-params">ev</span>)</span>{
<span class="hljs-keyword">if</span>(ev.which == <span class="hljs-number">13</span>){ <span class="hljs-comment">// ENTER key</span>
ev.preventDefault();
fire(ev); <span class="hljs-comment">// if key is enter , we fire the event;</span>
}
}
dom.on(elem, <span class="hljs-string">"keypress"</span>, update);
});
</code></pre>
<pre><code class="lang-html"><span class="hljs-comment"><!-- use in template --></span>
<span class="hljs-tag"><<span class="hljs-name">textarea</span> <span class="hljs-attr">on-enter</span>=<span class="hljs-string">{this.submit($event)}</span>></span><span class="hljs-tag"></<span class="hljs-name">textarea</span>></span>
</code></pre>
<p><strong>Tip:</strong> 模板中出现的 <code>this</code> 指向组件实例本身,回车后会运行实例的submit方法</p>
<script async src="//jsfiddle.net/leeluolee/3ac62L4g/embed/js,result/"></script>
<p>查看 <a href="#$event">$event</a>了解更多</p>
<blockquote>
<p>注意:除非是自定义事件,其它事件无论浏览器是否支持,都会通过addEventListener进行绑定</p>
</blockquote>
<h3 id="事件代理支持"><a name="事件代理支持" class="anchor-navigation-ex-anchor" href="#事件代理支持"><i class="fa fa-link" aria-hidden="true"></i></a>1.3. 事件代理支持</h3>
<p><code>on-*</code>会在当前节点绑定事件,在某些情况下(比如大列表),这种方式不是很高效。</p>
<p>在这种情况下,我们可以尝试使用 <code>delegate-</code> 代替 <code>on-</code>,来避免潜在的性能问题,<code>delegate-</code> 只会绑定一次事件到 父节点 上</p>
<p><strong>Example</strong></p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">delegate-click</span>=<span class="hljs-string">"remove"</span>></span>Delte<span class="hljs-tag"></<span class="hljs-name">div</span>></span> //Proxy way via delegate
<span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">delegate-click</span>=<span class="hljs-string">{this.remove()}</span>></span>Delte<span class="hljs-tag"></<span class="hljs-name">div</span>></span> // Evaluated way via delagate
</code></pre>
<p>但是,需要注意的是,<code>delegate-</code> 并非是「银弹」</p>
<ol>
<li>避免在高频触发的事件中使用( 比如 <code>mouseover</code> ),这样反而会产生性能问题</li>
<li>事件必须可冒泡<ul>
<li><a href="#custom-event">自定义事件</a>请参考 zepto tap-event 的<a href="https://github.com/madrobby/zepto/blob/master/src/event.jsL274" target="_blank">实现</a>.</li>
<li>部分事件天生没法冒泡,比如 IE 低版下的 <code>change</code>,<code>select</code> 等</li>
</ul>
</li>
</ol>
<h3 id="event"><a name="event" class="anchor-navigation-ex-anchor" href="#event"><i class="fa fa-link" aria-hidden="true"></i></a>1.4. $event 对象 </h3>
<p><code>$event</code> 会在每次事件触发时注入到 <code>data.$event</code> 中,你可以直接在模板里使用它。</p>
<p><strong>Example</strong></p>
<script async src="//jsfiddle.net/leeluolee/1o2gf4um/embed/js,result/"></script>
<p><code>$event</code>对象是被修正过的,在兼容 IE6 的前提下,你可以使用以下规范内的属性</p>
<ol>
<li>origin: 绑定节点</li>
<li>target: 触发节点</li>
<li>preventDefault(): 阻止事件默认行为</li>
<li>stopPropagation(): 阻止事件冒泡</li>
<li>which</li>
<li>pageX</li>
<li>pageY</li>
<li>wheelDelta</li>
<li>event: <strong>原始事件对象</strong></li>
</ol>
<blockquote>
<p>对于自定义事件,$event 即传入 fire 触发器的对象</p>
</blockquote>
<h2 id="组件事件"><a name="组件事件" class="anchor-navigation-ex-anchor" href="#组件事件"><i class="fa fa-link" aria-hidden="true"></i></a>2. 组件事件</h2>
<p>Regular 集成了一个轻量级的 Emitter,使得所有组件实例都可以使用以下接口来实现事件驱动的开发</p>
<ul>
<li><a href="../intro/hello.html#start">component.$on</a>: 用于添加事件监听器</li>
<li><a href="../intro/what.html#start">component.$off</a>: 用于解绑事件监听器</li>
<li><a href="../intro/what.html#start">component.$emit</a>: 用于触发某个事件</li>
</ul>
<p><strong>Example</strong></p>
<script async src="//jsfiddle.net/leeluolee/h03acpfy/1/embed/js,result/"></script>
<h3 id="模板里声明组件事件绑定"><a name="模板里声明组件事件绑定" class="anchor-navigation-ex-anchor" href="#模板里声明组件事件绑定"><i class="fa fa-link" aria-hidden="true"></i></a>2.1. 模板里声明组件事件绑定</h3>
<p>与DOM事件绑定类似,声明式组件以 <code>on-</code> 开头的属性都会被视作事件绑定</p>
<p>假设已经注册了一个名为 <code>Pager</code> 的翻页器组件</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">Pager</span> <span class="hljs-attr">on-nav</span>=<span class="hljs-string">{</span> <span class="hljs-attr">this.refresh</span>($<span class="hljs-attr">event</span>) } /></span>
</code></pre>
<script async src="//jsfiddle.net/leeluolee/8pfa43ms/embed/js,result/"></script>
<p>每当 <code>Pager</code> 组件通过 <code>$emit</code> 抛出<code>nav</code>事件,对应的 <code>this.refresh($event)</code> 就会被调用,执行组件上的 refresh 方法</p>
<p>是的,整个过程和 DOM 事件并无二样。但其中的 <code>$event</code> 代表 $emit 传入的<strong>第一个</strong>参数</p>
<h3 id="内建生命周期内的组件事件"><a name="内建生命周期内的组件事件" class="anchor-navigation-ex-anchor" href="#内建生命周期内的组件事件"><i class="fa fa-link" aria-hidden="true"></i></a>2.2. 内建生命周期内的组件事件</h3>
<p>Regular 会在组件实例初始化过程的关键阶段抛出事件</p>
<ol>
<li><code>$config</code>: 会在 compile 之前触发</li>
<li><code>$init</code> : 会在 compile 之后触发,此时,dom 结构已经生成,你可以通过 this.$refs 来获取了</li>
<li><code>$destroy</code>: 会在组件被 destroy 时触发</li>
</ol>
<blockquote>
<p><code>$</code>前缀是为了标识内建事件</p>
</blockquote>
<h2 id="事件的共性"><a name="事件的共性" class="anchor-navigation-ex-anchor" href="#事件的共性"><i class="fa fa-link" aria-hidden="true"></i></a>3. 事件的共性</h2>
<p>组件和DOM事件具有共性</p>
<h3 id="事件绑定语法"><a name="事件绑定语法" class="anchor-navigation-ex-anchor" href="#事件绑定语法"><i class="fa fa-link" aria-hidden="true"></i></a>3.1. 事件绑定语法</h3>
<ul>
<li><p>表达式(e.g. <code>on-click={this.remove()}</code>)</p>
<p>如果传入的是表达式,该表达式会在事件触发时被运行一次。</p>
<p><strong>Example</strong></p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">on-click</span>=<span class="hljs-string">{this.remove(index)}</span>></span>Delte<span class="hljs-tag"></<span class="hljs-name">div</span>></span>
</code></pre>
<p>这是一种最标准的写法,大多数时候你只会用到它</p>
</li>
<li><p>非表达式(e.g. <code>on-click="remove"</code>)</p>
<p>简化写法,等价于 <code>on-click={this.$emit('remove', $event)}</code> ,事件会以同样的事件名和参数被再次抛出,相当于做了一次代理</p>
<p><strong>Example</strong></p>
</li>
</ul>
<script async src="//jsfiddle.net/leeluolee/5hmawdcr/embed/js,result/"></script>
<h2 id="事件的不同点"><a name="事件的不同点" class="anchor-navigation-ex-anchor" href="#事件的不同点"><i class="fa fa-link" aria-hidden="true"></i></a>4. 事件的不同点</h2>
<ol>
<li>组件事件由 <code>$emit</code> 方法抛出,而 DOM 事件由浏览器抛出</li>
<li>DOM 事件由于 DOM 本身的特点,是可以冒泡的,而组件事件没有冒泡的机制</li>
<li><code>$event</code> 在组件事件中是 $emit 传入的第一个参数,而在 DOM 事件中是封装过的事件对象</li>
</ol>
<h2 id="完整的事件绑定流程"><a name="完整的事件绑定流程" class="anchor-navigation-ex-anchor" href="#完整的事件绑定流程"><i class="fa fa-link" aria-hidden="true"></i></a>5. 完整的事件绑定流程</h2>
<p><img src="https://p1.music.126.net/eHVTTZYK5El8xqv8Z5RcSg==/109951163389106097.png" alt=""></p>
</section>
</div>
<div class="search-results">
<div class="has-results">
<h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
<ul class="search-results-list"></ul>
</div>
<div class="no-results">
<h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
</div>
</div>
</div>
</div>
</div>
</div>
<a href="interpolation.html" class="navigation navigation-prev " aria-label="Previous page: ️插值">
<i class="fa fa-angle-left"></i>
</a>
<a href="statement/" class="navigation navigation-next " aria-label="Next page: ️语句">
<i class="fa fa-angle-right"></i>
</a>
</div>
<script>
var gitbook = gitbook || [];
gitbook.push(function() {
gitbook.page.hasChanged({"page":{"title":"️事件","level":"1.2.3","depth":2,"next":{"title":"️语句","level":"1.2.4","depth":2,"path":"basic/statement/README.md","ref":"basic/statement/README.md","articles":[{"title":"️列表渲染 - list","level":"1.2.4.1","depth":3,"path":"basic/statement/list.md","ref":"basic/statement/list.md","articles":[]},{"title":"️条件渲染 - if","level":"1.2.4.2","depth":3,"path":"basic/statement/if.md","ref":"basic/statement/if.md","articles":[]},{"title":"️结构引入 - include","level":"1.2.4.3","depth":3,"path":"basic/statement/include.md","ref":"basic/statement/include.md","articles":[]}]},"previous":{"title":"️插值","level":"1.2.2","depth":2,"path":"basic/interpolation.md","ref":"basic/interpolation.md","articles":[]},"dir":"ltr"},"config":{"plugins":["edit-link","splitter","-lunr","-search","-sharing","search-plus","expandable-chapters-small","anchor-navigation-ex","force-http"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"splitter":{},"force-http":{},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":false,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":true},"expandable-chapters-small":{},"edit-link":{"label":"编辑当前页","base":"https://github.com/regularjs/guide/edit/master/"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"search-plus":{}},"theme":"default","author":"leeluolee","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Regular官方指南","language":"zh-hans","links":{"sidebar":{"Github":"https://github.com/regularjs/regular"}},"gitbook":"3.2.3","description":"Regular官方指南"},"file":{"path":"basic/event.md","mtime":"2018-08-20T07:57:49.405Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-08-23T07:40:58.484Z"},"basePath":"..","book":{"language":""}});
});
</script>
</div>
<script src="../gitbook/gitbook.js"></script>
<script src="../gitbook/theme.js"></script>
<script src="../gitbook/gitbook-plugin-edit-link/plugin.js"></script>
<script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
<script src="../gitbook/gitbook-plugin-search-plus/jquery.mark.min.js"></script>
<script src="../gitbook/gitbook-plugin-search-plus/search.js"></script>
<script src="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
<script src="../gitbook/gitbook-plugin-force-http/test.js"></script>
<script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
</body>
</html>