简体   繁体   English

Angular2 RC6 父子组件

[英]Angular2 RC6 Parent Child Components

I've looked at the other Angular2 RC6 parent child posts but none of them solve the issue I am having.我查看了其他 Angular2 RC6 父子帖子,但没有一个能解决我遇到的问题。

I've got my main AppModule that imports a HomeModule.我有导入 HomeModule 的主 AppModule。 That seems to work.这似乎有效。 Now I've added a MapModule that implements angular2-google-maps as an import into my HomeModule and I am getting the error:现在我添加了一个 MapModule,它实现了 angular2-google-maps 作为导入到我的 HomeModule 中,我收到了错误:

'google-map' is not a known element:
1. If 'google-map' is an Angular component, then verify that it is part of this module.
2. If 'google-map' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schema' of this component to suppress this message. ("<h1>home</h1>[ERROR ->]<google-map></google-map>"): HomeComponent@0:13

Here is my setup:这是我的设置:

// ----------
// AppModule
// ----------
@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        ReactiveFormsModule,
        HttpModule,
        HomeModule,
        routing
    ],
    declarations: [
        AppComponent,
    ],
    // schemas: [CUSTOM_ELEMENTS_SCHEMA],
    providers: [
        {provide: APP_BASE_HREF, useValue : '/' },
        {provide: LocationStrategy, useClass: HashLocationStrategy}
    ],
    bootstrap: [AppComponent]
})
export class AppModule {}

// ----------
// HomeModule
// ----------
@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        ReactiveFormsModule,
        HttpModule,
        MapModule,
    ],
    declarations: [
        HomeComponent
    ],
    bootstrap: [HomeComponent]
})
export class HomeModule {}

// ----------
// HomeComponent
// ----------
@Component({
    selector: 'home',
    template: "<h1>home</h1><google-map></google-map>",
    styleUrls: ['home.scss'],
})
export class HomeComponent {}

// ----------
// MapModule
// ----------
@NgModule({
    imports: [
        BrowserModule,
        CommonModule,
        FormsModule,
        AgmCoreModule.forRoot({
            apiKey: '<my-api-key>'
        })
    ],
    declarations: [MapComponent],
    providers: [MapService],
    bootstrap: [MapComponent]
})
export class MapModule {}

// ----------
// Map Component
// ----------
@Component({
    selector: 'google-map',
    templateUrl: 'map.component.html',
    styleUrls: ['map.scss']
})
export class MapComponent {}

adding CUSTOM_ELEMENTS_SCHEMA to the module get's rid of the error but the map doesn't render.将 CUSTOM_ELEMENTS_SCHEMA 添加到模块可以消除错误,但地图不会呈现。

If I make the MapModule part of the AppModule directly instead of as a child to the HomeModule then I see the map.如果我将 MapModule 直接作为 AppModule 的一部分而不是作为 HomeModule 的子项,那么我会看到地图。

Any clues?有什么线索吗?


****Edit**** ****编辑****


Thanks for the comments so far.感谢您到目前为止的评论。 My code looks cleaner now.我的代码现在看起来更干净了。 I've applied the following changes based on comments but the error remains the same.我根据评论应用了以下更改,但错误保持不变。 My new code looks like the following.我的新代码如下所示。

// ----------
// AppModule
// ----------
@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        ReactiveFormsModule,
        HttpModule,
        HomeModule,
        routing
    ],
    declarations: [
        AppComponent,
    ],
    // schemas: [CUSTOM_ELEMENTS_SCHEMA],
    providers: [
        {provide: APP_BASE_HREF, useValue : '/' },
        {provide: LocationStrategy, useClass: HashLocationStrategy}
    ],
    bootstrap: [AppComponent]
})
export class AppModule {}

// ----------
// HomeModule
// ----------
@NgModule({
    imports: [
        CommonModule,
        MapModule,
    ],
    declarations: [
        HomeComponent
    ]
})
export class HomeModule {}

// ----------
// HomeComponent
// ----------
@Component({
    selector: 'home',
    template: "<h1>home</h1><google-map></google-map>",
    styleUrls: ['home.scss'],
})
export class HomeComponent {}

// ----------
// MapModule
// ----------
@NgModule({
    imports: [
        CommonModule,
        AgmCoreModule.forRoot({
            apiKey: '<my-api-key>'
        })
    ],
    declarations: [MapComponent],
    providers: [MapService]
})
export class MapModule {}

// ----------
// Map Component
// ----------
@Component({
    selector: 'google-map',
    templateUrl: 'map.component.html',
    styleUrls: ['map.scss']
})
export class MapComponent {}

Additionally I checked out the link provided to the other Stack Overflow issue and I noticed they had a parent-child example but in their example the child did not have a module tied to it.此外,我查看了提供给其他 Stack Overflow 问题的链接,我注意到他们有一个父子示例,但在他们的示例中,孩子没有与之绑定的模块。 The MapComponent in my example is pretty complex with ties to the angular2-google-map module and seems like it should have it's own module to handle that.我的示例中的 MapComponent 与 angular2-google-map 模块的关系非常复杂,似乎它应该有自己的模块来处理它。


** Edit 2 ** ** 编辑 2 **


I've added a plunker post to replicate my problem.我添加了一个 plunker 帖子来复制我的问题。 http://plnkr.co/edit/7M71N6ttYR2s9yNdB3UT?p=preview http://plnkr.co/edit/7M71N6ttYR2s9yNdB3UT?p=preview

Note that only the app.module should import BrowserModule and have a bootstrap property.请注意,只有app.module应该导入BrowserModule并具有bootstrap属性。 Can you remove those from the modules and see if that helped?你能从模块中删除那些,看看是否有帮助? Keep importing CommonModule as you did in your MapModule for all non app.module modules.继续像在MapModule为所有非app.module模块导入CommonModule一样。

Also, no need for duplicate imports of FormsModule , HttpModule and ReactiveFormsModule .此外,无需重复导入FormsModuleHttpModuleReactiveFormsModule It's suffice that the app.module imports them. app.module导入它们就足够了。

ps sorry for commenting and then answering, I have a strange bug in chrome in the answer box that for some reason causes the header pane to duplicate itself over the box :( ps 抱歉评论然后回答,我在回答框中的 chrome 中有一个奇怪的错误,由于某种原因导致标题窗格在框上复制自己:(

在此处输入图片说明

I found the solution to my issue.我找到了我的问题的解决方案。 I needed to add the exports to my MapModule so that other components could use the component.我需要将导出添加到我的 MapModule,以便其他组件可以使用该组件。 My final change was to MapModule as follows我的最终更改是 MapModule 如下

// ----------
// MapModule
// ----------
@NgModule({
    imports: [
        CommonModule,
        AgmCoreModule.forRoot({
            apiKey: '<my-api-key>'
        })
    ],
    declarations: [MapComponent],
    exports: [MapComponent],
    providers: [MapService]
})
export class MapModule {}

exports: [MapComponent]出口:[地图组件]

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

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