简体   繁体   English

扩展Angular2指令

[英]Extend Angular2 Directive

I am trying to extend the angular2-clipboard npm package. 我正在尝试扩展angular2-clipboard npm包。 I need to get access to its ngOnInit() function and override it to suit a specific use case for copying. 我需要访问其ngOnInit()函数并重写它以适合特定的用例进行复制。

I am new to angular2 and unsure how to do this, the way I've been trying it so far is linked in the plunker below. 我是angular2的新手,不确定如何执行此操作,到目前为止,我一直在尝试的方法与下面的链接有关。 I am having difficulty since the package is exported as a module called ClipboardModule and I need the directive. 由于将包导出为名为ClipboardModule的模块,因此我遇到了困难,我需要使用指令。

Here is the plugin github and plunker for reference: 这是插件github和plunker供参考:

  1. Github: https://github.com/maxisam/angular2-clipboard GitHub: https : //github.com/maxisam/angular2-clipboard
    • The file that shows the export is src/clipboard.module.ts 显示导出的文件是src / clipboard.module.ts
    • Their github also has a link to a plunker example of it working 他们的github也有一个指向它工作的更简单示例的链接
  2. My plunker: https://embed.plnkr.co/sIxmFo/ 我的朋克: https ://embed.plnkr.co/sIxmFo/

This is how one defines a service, not a directive. 这就是定义服务而不是指令的方式。

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

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

A directive requires a @Directive() decorator instead of the @Injectable() 指令需要@Directive()装饰器,而不是@Injectable()

Inheritance with components, directives, and pipes isn't supported currently. 当前不支持使用组件,指令和管道进行继承。

I also don't understand why you extend a module when you want to extend a directive. 我也不明白为什么要扩展指令时扩展模块。 You need to extend the directive class directly. 您需要直接扩展指令类。

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

Some scenarios can work though. 但是某些情况下可以使用。 Usually you need to repeat all decorators in the subclass. 通常,您需要重复子类中的所有装饰器。

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

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

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