繁体   English   中英

扩展Angular2指令

[英]Extend Angular2 Directive

我正在尝试扩展angular2-clipboard npm包。 我需要访问其ngOnInit()函数并重写它以适合特定的用例进行复制。

我是angular2的新手,不确定如何执行此操作,到目前为止,我一直在尝试的方法与下面的链接有关。 由于将包导出为名为ClipboardModule的模块,因此我遇到了困难,我需要使用指令。

这是插件github和plunker供参考:

  1. GitHub: https : //github.com/maxisam/angular2-clipboard
    • 显示导出的文件是src / clipboard.module.ts
    • 他们的github也有一个指向它工作的更简单示例的链接
  2. 我的朋克: https ://embed.plnkr.co/sIxmFo/

这就是定义服务而不是指令的方式。

@Injectable()
export default class CopyDirective extends ClipboardModule {
        public cm: any; // better way than any?

    constructor(cm: ClipboardModule) {
        super();
        this.cm = cm;
    }
}

指令需要@Directive()装饰器,而不是@Injectable()

当前不支持使用组件,指令和管道进行继承。

我也不明白为什么要扩展指令时扩展模块。 您需要直接扩展指令类。

另请参阅https://github.com/angular/angular/issues/11606

但是某些情况下可以使用。 通常,您需要重复子类中的所有装饰器。

@Component()@Directive()@Input()@Output()@ViewChild(ren)()@ContentChild(ren)() ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM