如何使指令和组件在全球范围内可用 [英] How to make directives and components available globally

查看:87
本文介绍了如何使指令和组件在全球范围内可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个自定义指令,在Angular 2应用程序中使用它关闭了Angular 2应用程序所有不同组件的内容面板(模板中的某些内容所有者).由于每个组件的代码完全相同,因此我认为编写一条可以定义一次并在所有组件中使用的指令是有意义的.这是我的指令的样子:

I wrote a custom directive that I use in my Angular 2 application to close content panels (some content holders in my template) in all the different components of my Angular 2 application. Since this code is quite the same for each component, I thought that I would make sense to write a directive that I could define once, and use in all components. This is what my directive looks like:

import { Directive, ElementRef, HostListener, Injectable } from '@angular/core';

@Directive({
    selector: '[myCloseContentPanel]'
})

export class CloseContentPanelDirective {
    private el: HTMLElement;

    constructor(el: ElementRef) {
        this.el = el.nativeElement;
    }

    @HostListener('click') onMouseClick() {
        this.el.style.display = 'none';
    }
}

现在,我希望可以一​​次在app.component父组件中导入此指令,然后可以在所有子组件中使用此指令.遗憾的是,这行不通,因此我必须在每个组件中分别导入此指令.难道我做错了什么?还是这种行为根本不可能?

Now I expected that I could import this directive once in a app.component parent component, and that I then could use this directive throughout all the child components. This sadly doesn't work, so I would have to import this directive in each component separately. Am I doing something wrong? Or is this behaviour simply not possible?

推荐答案

更新> = RC.5

您必须在要使用导入模块的组件,指令或管道的任何模块中导入模块.没有办法解决.

You have to import a module in whatever module you want to use components, directives or pipes of the imported module. There is no way around it.

您可以做的是创建一个导出其他几个模块的模块(例如,导出CommonModuleBrowserModule.

What you can do is to create a module that exports several other modules (for instance, the BrowserModule that exports CommonModule.

@NgModule({
  declarations: [CoolComponent, CoolDirective, CoolPipe],
  imports: [MySharedModule1, MySharedModule2],
  exports: [MySharedModule1, MySharedModule2, CoolComponent, CoolDirective, CoolPipe],
})
export class AllInOneModule {}

@NgModule({
  imports: [AllInOneModule]
})
class MyModule {}

通过这种方式,您可以将AllInOneModule导出的所有内容提供给MyModule.

This way you make everything exported by AllInOneModule available to MyModule.

另请参见 https://angular.io/docs/ts/latest/guide/ngmodule.html

更新< = RC.5

bootstrap(AppComponent, [provide(PLATFORM_DIRECTIVES, {useValue: [CloseContentPanelDirective], multi: true})]);

请参阅以下注释-即使根组件中的每个样式指南providersboostrap()都更受青睐,这还是行不通的:

See comments below - even though per style guide providers in the root component should be favored over boostrap() this doesn't work:

原始

在根组件上添加

On the root component add

@Component({
  selector: 'my-app',
  providers: [provide(PLATFORM_DIRECTIVES, {useValue: [CloseContentPanelDirective], multi: true})],
  templat: `...`
})
export component AppComponent {
}

@Component()@Directive()@Pipe()已包含@Injectable().无需在此添加它.

@Component(), @Directive(), @Pipe() already include @Injectable(). No need to add it there as well.

这篇关于如何使指令和组件在全球范围内可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆