【问题标题】:Angular2 RC6 Parent Child ComponentsAngular2 RC6 父子组件
【发布时间】:2016-09-14 13:22:19
【问题描述】:

我查看了其他 Angular2 RC6 父子帖子,但没有一个能解决我遇到的问题。

我的主 AppModule 导入了 HomeModule。这似乎行得通。现在我添加了一个 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

这是我的设置:

// ----------
// 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 {}

将 CUSTOM_ELEMENTS_SCHEMA 添加到模块可以消除错误,但地图不会呈现。

如果我直接将 MapModule 作为 AppModule 的一部分而不是作为 HomeModule 的子级,那么我会看到地图。

有什么线索吗?


****编辑****


感谢到目前为止的 cmets。我的代码现在看起来更干净了。我已经根据 cmets 应用了以下更改,但错误保持不变。我的新代码如下所示。

// ----------
// 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 {}

此外,我查看了提供给其他 Stack Overflow 问题的链接,我注意到他们有一个父子示例,但在他们的示例中,孩子没有与之关联的模块。我的示例中的 MapComponent 与 angular2-google-map 模块的关系非常复杂,并且似乎它应该有自己的模块来处理它。


** 编辑 2 **


我添加了一个 plunker 帖子来复制我的问题。 http://plnkr.co/edit/7M71N6ttYR2s9yNdB3UT?p=preview

【问题讨论】:

  • 请注意,只有app.module 应该导入BrowserModule 并具有bootstrap 属性。您可以从模块中删除它们,看看是否有帮助?此外,无需重复导入 FormsModuleHttpModuleReactiveFormsModuleapp.module 导入它们就足够了。
  • 要扩展@mrgoos提示,其他模块应该导入CommonModule而不是BrowserModuleBrowserModule导出CommonModule,因此如果你导入BrowserModule你不需要添加CommonModule 再次)。
  • 是的。就像他在MapModule 所做的那样。
  • 我已经清理了我的代码并将其发布为编辑。我的代码看起来更干净,但错误仍然存​​在。

标签: angular


【解决方案1】:

请注意,只有 app.module 应该导入 BrowserModule 并具有 bootstrap 属性。您可以从模块中删除它们,看看是否有帮助?像在 MapModule 中对所有非 app.module 模块所做的那样,继续导入 CommonModule

此外,无需重复导入 FormsModuleHttpModuleReactiveFormsModuleapp.module 导入它们就足够了。

附言很抱歉发表评论然后回答,我在答案框中的 chrome 中有一个奇怪的错误,由于某种原因导致标题窗格在框上复制自身:(

【讨论】:

    【解决方案2】:

    我找到了解决问题的方法。我需要将导出添加到我的 MapModule,以便其他组件可以使用该组件。我最后的改变是 MapModule 如下

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

    导出:[MapComponent]

    【讨论】:

    • 啊,是的。你需要它来使这个组件“公开”。 kk.
    猜你喜欢
    • 2017-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-18
    • 1970-01-01
    • 2017-04-21
    • 1970-01-01
    相关资源
    最近更新 更多