国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Angular中自定義Debounce Click指令防止重復點擊

2019-11-19 15:59:11
字體:
來源:轉載
供稿:網友

在這篇文章中,我們將介紹使用 Angular Directive API 來創建自定義 debounce click 指令。該指令將處理在指定時間內多次點擊事件,這有助于防止重復的操作。

對于我們的示例,我們希望在產生點擊事件時,實現去抖動處理。接下來我們將介紹 Directive API,HostListener API 和 RxJS 中 debounceTime 操作符的相關知識。首先,我們需要創建 DebounceClickDirective 指令并將其注冊到我們的 app.module.ts 文件中:

import { Directive, OnInit } from '@angular/core';@Directive({ selector: '[appDebounceClick]'})export class DebounceClickDirective implements OnInit { constructor() { } ngOnInit() { }}@NgModule({ imports: [BrowserModule], declarations: [  AppComponent,  DebounceClickDirective ], providers: [], bootstrap: [AppComponent]})export class AppModule { }

Angular 指令是沒有模板的組件,我們將使用以下方式應用上面的自定義指令:

<button appDebounceClick>Debounced Click</button>

在上面 HTML 代碼中的宿主元素是按鈕,接下來我們要做的第一件事就是監聽宿主元素的點擊事件,因此我們可以將以下代碼添加到我們的自定義指令中。

import { Directive, HostListener, OnInit } from '@angular/core';@Directive({ selector: '[appDebounceClick]'})export class DebounceClickDirective implements OnInit { constructor() { } ngOnInit() { } @HostListener('click', ['$event']) clickEvent(event: MouseEvent) {  event.preventDefault();  event.stopPropagation();  console.log('Click from Host Element!'); }}

在上面的例子中,我們使用了 Angular @HostListener 裝飾器,該裝飾器允許你輕松地監聽宿主元素上的事件。在我們的示例中,第一個參數是事件名。第二個參數 $event,這用于告訴 Angular 將點擊事件傳遞給我們的 clickEvent() 方法。

在事件處理函數中,我們可以調用 event.preventDefault()event.stopPropagation() 方法來阻止瀏覽器的默認行為和事件冒泡。

Debounce Events

現在我們可以攔截宿主元素的 click 事件,此時我們還需要有一種方法實現事件的去抖動處理,然后將它重新發送回父節點。這時我們需要借助事件發射器和 RxJS 中的 debounce 操作符。

import { Directive, EventEmitter, HostListener, OnInit, Output } from '@angular/core';import { Subject } from 'rxjs/Subject';import 'rxjs/add/operator/debounceTime';@Directive({  selector: '[appDebounceClick]'})export class DebounceClickDirective implements OnInit {  @Output() debounceClick = new EventEmitter();  private clicks = new Subject<any>();  constructor() { }  ngOnInit() {    this.clicks      .debounceTime(500)      .subscribe(e => this.debounceClick.emit(e));  }  @HostListener('click', ['$event'])  clickEvent(event: MouseEvent) {    event.preventDefault();    event.stopPropagation();    this.clicks.next(event);  }}

在上面的代碼中,我們使用 Angular @Output 屬性裝飾器和 EventEmitter 類,它們允許我們在指令上創建自定義事件。要發出事件,我們需要調用 EventEmitter 實例上的 emit() 方法。

但我們不想立即發出點擊事件,我們想做去抖動處理。為了實現這個功能,我們將使用 RxJS 中的 Subject 類。在我們的代碼中,我們創建一個主題來處理我們的點擊事件。在我們的方法中,我們調用 next() 方法來讓 Subject 對象發出下一個值。此外我們也使用 RxJS 中 debounceTime 的操作符,這允許我們通過設置給定的毫秒數來去抖動點擊事件。

一旦我們設置好了,我們現在可以在下面的模板中監聽我們的自定義去抖動點擊事件。

<button appDebounceClick (debounceClick)="log($event)"> Debounced Click</button>

現在,當我們點擊我們的按鈕時,它將延遲 500 毫秒。 500毫秒后,我們的自定義輸出屬性將會發出點擊事件。現在我們有了基本的功能,我們需要做一些清理工作,并增加一些其它的功能。

Unsubscribe

對于 RxJS 中 Observables 和 Subject 對象,一旦我們不再使用它們,我們必須取消訂閱事件。如果我們沒有執行取消訂閱操作,有可能會出現內存泄漏。

import { Directive, EventEmitter, HostListener, OnInit, Output, OnDestroy } from '@angular/core';import { Subject } from 'rxjs/Subject';import { Subscription } from "rxjs/Subscription";import 'rxjs/add/operator/debounceTime';@Directive({  selector: '[appDebounceClick]'})export class DebounceClickDirective implements OnInit, OnDestroy {  @Output() debounceClick = new EventEmitter();  private clicks = new Subject<any>();  private subscription: Subscription;  constructor() { }  ngOnInit() {    this.subscription = this.clicks      .debounceTime(500)      .subscribe(e => this.debounceClick.emit(e));  }  ngOnDestroy() {    this.subscription.unsubscribe();  }  @HostListener('click', ['$event'])  clickEvent(event: MouseEvent) {    event.preventDefault();    event.stopPropagation();    this.clicks.next(event);  }}

要取消訂閱,我們需要保存訂閱時返回的訂閱對象。當 Angular 銷毀組件時,它將調用 OnDestroy 生命周期鉤子,因此我們可以在這個鉤子中,執行取消訂閱操作。

Custom Inputs

我們指令的功能已基本齊全,它可以正常處理事件。接下來,我們將添加一些更多的邏輯,以便我們可以自定義去抖動時間。為此,我們將使用 @Input 裝飾器。

import { Directive, EventEmitter, HostListener, OnInit, Output, OnDestroy, Input } from '@angular/core';import { Subject } from 'rxjs/Subject';import { Subscription } from "rxjs/Subscription";import 'rxjs/add/operator/debounceTime';@Directive({  selector: '[appDebounceClick]'})export class DebounceClickDirective implements OnInit, OnDestroy {  @Input() debounceTime = 500;  @Output() debounceClick = new EventEmitter();  private clicks = new Subject<any>();  private subscription: Subscription;  constructor() { }  ngOnInit() {    this.subscription = this.clicks      .debounceTime(this.debounceTime)      .subscribe(e => this.debounceClick.emit(e));  }  ngOnDestroy() {    this.subscription.unsubscribe();  }  @HostListener('click', ['$event'])  clickEvent(event: MouseEvent) {    event.preventDefault();    event.stopPropagation();    this.clicks.next(event);  }}

@Input 裝飾器允許我們將自定義延遲時間傳遞到我們的組件或指令中。在上面的代碼中,我們可以通過組件的輸入屬性,來指定我們希望去抖動的時間。默認情況下,我們將其設置為 500 毫秒。

<button appDebounceClick (debounceClick)="log($event)" [debounceTime]="300"> Debounced Click</button>

參考資源

creating-a-custom-debounce-click-directive-in-angular

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 遂宁市| 奉贤区| 黄大仙区| 新津县| 永嘉县| 临夏市| 新龙县| 社会| 新郑市| 阳城县| 十堰市| 武山县| 醴陵市| 鄱阳县| 云梦县| 许昌县| 连平县| 锦州市| 忻州市| 修文县| 利川市| 策勒县| 邹城市| 桃园市| 德庆县| 夏津县| 江津市| 北安市| 西平县| 固安县| 临江市| 通河县| 三穗县| 蓝田县| 英山县| 长汀县| 镇宁| 神池县| 邵武市| 平舆县| 丰城市|