脚踩小金瓶 發表於 2020-3-14 00:22:00

踩坑实录---Angular防抖——点击事件

<div class="cnblogs_code">
<pre>npx ng g directive DebounceClickDirective --module=app</pre>
</div>
<p>然后自动生成了2 个文件</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">CREATE src/app/debounce-click-directive.directive.spec.ts (290 bytes)
CREATE src/app/debounce-click-directive.directive.ts (173 bytes)</span></pre>
</div>
<p>检查一下</p>
<pre><span>debounce-click-directive.directive.spec.ts</span><br><br></pre>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">import {
Directive,
OnInit,
HostListener,
Output,
EventEmitter,
OnDestroy,
Input, HostBinding
} from '@angular/core';
import { Subject, Subscription } from 'rxjs';
import { debounceTime } from 'rxjs/operators';

@Directive({
selector: ''
})
export class DebounceClickDirective implements OnInit, OnDestroy {
@Input('appDebounceClick') debounceTime = 500;
@Output() debounceClick = new EventEmitter();
private clicks = new Subject</span><span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">any</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span><span style="color: rgba(0, 0, 0, 1)">();
private subscription: Subscription;

constructor() {
}

ngOnInit() {
    this.subscription = this.clicks.pipe(
      debounceTime(this.debounceTime)
    ).subscribe(e =&gt; this.debounceClick.emit(e));
}

ngOnDestroy() {
    this.subscription.unsubscribe();
}

@HostListener('click', ['$event'])
clickEvent(event) {
    event.preventDefault();
    event.stopPropagation();
    this.clicks.next(event);
}

@HostBinding()
test() {
    //
}
}</span></pre>
</div>
<p>再检查一下app.module.ts</p>
<pre><span>src/app/</span></pre>
<div>
<div>&nbsp;</div>
<div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)"><strong><span style="color: rgba(255, 0, 0, 1)">import { DebounceClickDirective } from './debounce-click-directive.directive';</span></strong>

@NgModule({
declarations: ,</span></pre>
</div>
<p>注意啊!!这里有个坑,有的项目是分模块的,注册到app.module有的时候也是不管用的,你需要<span style="font-size: 14pt"><strong><span style="color: rgba(255, 0, 0, 1)">注册到距离你需要用到的最近的模块</span></strong></span>,因为这个是按需引入的,</p>
<p>要不然你这个自定义指令是没卵用的哦!!!!</p>
<p>然后很简单</p>
<p>你的html就可以直接使用了</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">button </span><span style="color: rgba(255, 0, 0, 1)">(click)</span><span style="color: rgba(0, 0, 255, 1)">="myappDebounceClick()"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>即刻執行<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">button</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">button </span><span style="color: rgba(255, 0, 0, 1)">appDebounceClick (debounceClick)</span><span style="color: rgba(0, 0, 255, 1)">="myappDebounceClick()"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>使用默認時間間隔來執行<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">button</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">button </span><span style="color: rgba(255, 0, 0, 1)">appDebounceClick (debounceClick)</span><span style="color: rgba(0, 0, 255, 1)">="myappDebounceClick()"</span><span style="color: rgba(255, 0, 0, 1)"> </span><span style="color: rgba(0, 0, 255, 1)">="2000"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span><span style="color: rgba(0, 0, 0, 1)">自定義時間執行Debounced12
    Click</span><span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">button</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<p>再放一次自定义指令文件代码</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">import { Directive, EventEmitter, HostListener, OnInit, Output, Input } from '@angular/core';
import { Subject } from 'rxjs';
import { debounceTime } from 'rxjs/operators';
import { Subscription } from 'rxjs';

@Directive({
selector: ''
})
export class DebounceClickDirective implements OnInit {
@Input() debounceTime = 500;
@Output() debounceClick = new EventEmitter();
private clicks = new Subject();
private subscription: Subscription;

constructor() { }

ngOnInit() {
    this.subscription = this.clicks.pipe(
      debounceTime(this.debounceTime)
    ).subscribe(e =&gt; this.debounceClick.emit(e));
}

ngOnDestroy() {
    this.subscription.unsubscribe();
}

@HostListener('click', ['$event'])
clickEvent(event) {
    event.preventDefault();
    event.stopPropagation();
    this.clicks.next(event);
}
}</span></pre>
</div>
<p>&nbsp;</p>
</div>
</div>
<p>好了,完成了~~&nbsp; &nbsp; 三种情况自己看吧~</p>
<p></audio></p><br><br>
来源:https://www.cnblogs.com/sugartang/p/12485053.html
頁: [1]
查看完整版本: 踩坑实录---Angular防抖——点击事件