防抖指令

1.新建debounce-click 指令

2.directives/debounce-click.directive.ts

import { Directive, EventEmitter, HostListener, Input, OnInit, Output } from '@angular/core';
import { Subject, Subscription } from 'rxjs';
import { debounceTime } from 'rxjs/operators';

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

  constructor() {

  }

  ngOnInit(): void {
    this.subscription = this.clicks.pipe(
      debounceTime(this.debounceTime)
    ).subscribe(e => this.debounceClick.emit(e));
  }

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

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

}

3.如何使用

新建公共ts文件 modules/share/share.module.ts 将所有指令及管道引入

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { DebounceClickDirective } from '../../directives/debounce-click.directive';
import { HtmlpipePipe } from '../../pipe/htmlpipe.pipe';

@NgModule({
    declarations: [
        DebounceClickDirective,
        HtmlpipePipe
    ],
    imports: [
        CommonModule
    ],
    exports: [
        DebounceClickDirective,
        HtmlpipePipe
    ]
})
export class ShareModule { }

4.在login页面login.module.ts文件中引入ShareModule

// 防抖指令
import { ShareModule } from '../../modules/share/share.module';
imports: [
    ShareModule
  ],

5.login.page.html页面使用

<ion-button class="login_btn" shape="round" expand="block" appDebounceClick [debounceTime]="500"
      (debounceClick)="goPwdLogin()" [ngClass]="{'corect':ajxphone && ajxpwd}" disabled="{{!ajxphone || !ajxpwd}}">登录
    </ion-button>

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值