class 'ModalDirective' 错误地实现了接口 'AfterViewInit'

class 'ModalDirective' incorrectly implements interface 'AfterViewInit'

我正在尝试向我的项目添加模式,所以我找到了这个库:ng2-bootstrap

我首先使用以下命令安装它:npm install ng2-bootstrap --save

我的 Class 看起来像:

import { Directive, ElementRef, Input, Renderer, AfterViewInit, OnDestroy } from 
@angular/core';
import { ModalModule } from 'ng2-bootstrap/ng2-bootstrap';

@Directive({
  selector: '[bsModal]',
  exportAs: 'bs-modal'
})
export class ModalDirective implements AfterViewInit, OnDestroy {
  @Input()
  public set config(conf:ModalOptions) {
    this._config = this.getConfig(conf);
  };

  @Output() public onShow:EventEmitter<ModalDirective> = new EventEmitter();
  @Output() public onShown:EventEmitter<ModalDirective> = new EventEmitter();
  @Output() public onHide:EventEmitter<ModalDirective> = new EventEmitter();
  @Output() public onHidden:EventEmitter<ModalDirective> = new EventEmitter();

}

但是我得到了这个错误:

class 'ModalDirective' incorrectly implements interface 'AfterViewInit'

如果使用implements AfterViewInit, OnDestroy,则需要实现接口方法

export class ModalDirective implements AfterViewInit, OnDestroy {

  ngAfterViewInit() {
  }

  ngOnDestroy() {
  }
}

如果您不需要在这些生命周期钩子中做任何事情,那么只需删除 implements AfterViewInit, OnDestroy

另请参阅:

我在 ng2-bootstrap 文档中找到。

我认为您需要像这样添加您的组件:

import { Component, ViewChild } from '@angular/core';

    // todo: change to ng2-bootstrap
    import { ModalDirective } from '../../../components/modal/modal.component';
    @Component({
      selector: 'modal-demo',
      template: template
    })
    export class ModalDemoComponent {
      @ViewChild('childModal') public childModal:ModalDirective;

      public showChildModal():void {
        this.childModal.show();
      }

      public hideChildModal():void {
        this.childModal.hide();
      }
    }

如果你使用实现 OnInit, OnDestroy 方法那么你应该实现它的接口

export class ModalDirective implements OnInit, OnDestroy {

  ngOnInit() {
  }

  ngOnDestroy() {
  }
}