简体   繁体   English

Angular2组件如何导入指令组件?

[英]Angular2 Component How to Import Directive Component?

I just upgraded to angular2 final release and there were alot of breaking changes from rc4. 我刚升级到angular2最终版本,rc4有很多重大变化。 One of which is there is no longer a Directives collection on the component... I uesd to do something like this: 其中一个是组件上不再有Directives集合......我想做这样的事情:

@Component({
    selector: 'resource-tab',
    templateUrl: './app/applicationMgmt/resource/resource-tab.html',
    directives: [
        NgSwitch,
        NgSwitchCase,
        NgSwitchDefault,
        OtherResourceEditorComponent,
        MvcResourceEditorComponent,
        WcfResourceEditorComponent,
        WebResourceEditorComponent,
        WebApiResourceEditorComponent,
        ConfigResourceEditorComponent
    ]    
})

to reference other components that are used as directives on the view template... How does one do this now? 引用在视图模板上用作指令的其他组件...现在如何做到这一点?

The best way to do it would be to create a ResourceEditorModule like this: 最好的方法是创建一个这样的ResourceEditorModule

@NgModule({
    declarations:[
            OtherResourceEditorComponent,
            MvcResourceEditorComponent,
            WcfResourceEditorComponent,
            WebResourceEditorComponent,
            WebApiResourceEditorComponent,
            ConfigResourceEditorComponent
    ],
    exports:[
            OtherResourceEditorComponent,
            MvcResourceEditorComponent,
            WcfResourceEditorComponent,
            WebResourceEditorComponent,
            WebApiResourceEditorComponent,
            ConfigResourceEditorComponent
    ]
})
export class ResourceEditorModule{}

and add ResourceEditorModule as import in your AppModule : 并在AppModule中将ResourceEditorModule添加为import:

@NgModule({
    imports:[
        ...
        ...
        ResourceEditorModule
    ],
    ...
    ...
    ...
})
export class AppModule{}

The idea bedhind modules is to split your components, directives, pipes and services in groups that belongs to the same "package", like everything you need to use http for HttpModule , everything you need to use forms for FormsModule 模块背后的想法是将组件,指令,管道和服务拆分为属于同一“包”的组,就像您为HttpModule使用http所需的一切,使用FormsModule表单所需的FormsModule

You should specify Components, directives and Pipes under declarations property of the NgModule. 您应该在NgModule的声明属性下指定组件,指令和管道。 So basically the easiest way for me upgrading my angular 2 project to 2.0.0. 所以基本上最简单的方法是将我的角度2项目升级到2.0.0。 version was defining one module for the app (app.module.ts) put all there and then start breaking it smaller modules. 版本为应用程序(app.module.ts)定义了一个模块,然后开始打破它的小模块。 Good reference for this can be found here 这个很好的参考,可以发现这里

In Angular2.0.0 in-built directives are accessible/available with BrowserModule ,. 在Angular2.0.0 中,内置指令可通过BrowserModule访问/使用。 You just need to import it as shown below and you'll be able to most of in-built directives. 您只需要导入它,如下所示,您将能够使用大多数内置指令。

For customDirective, you need to declared them with declarations metadata as shown, 对于customDirective,您需要使用声明元数据声明它们,如图所示,

    import { NgModule }      from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';

   /*---------import all custom directives-----------*/

   import { OtherResourceEditorComponent} from 'valid path';
   import { MvcResourceEditorComponent } from 'valid path';
   ...
   ...   

    @NgModule({
      imports:      [ BrowserModule],
      declarations: [ AppComponent,OtherResourceEditorComponent,MvcResourceEditorComponent,... ],
      bootstrap:    [ AppComponent ]
    })
    export class AppModule { }

As others have stated you need to move your directives and providers to a module for your app. 正如其他人所说,您需要将指令和提供程序移动到应用程序的模块中。 The RC4 to RC5 migration guide explains how to do this, and some other potential problems. RC4到RC5迁移指南解释了如何执行此操作以及其他一些潜在问题。

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

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