【问题标题】:Angular2: Nesting a child component doesn't workAngular2:嵌套子组件不起作用
【发布时间】:2017-07-29 18:49:40
【问题描述】:

我已经玩了 angular 2 几天了。我正在尝试将子选择器插入父模板。它应该很简单,但我一辈子都无法让它发挥作用。我收到以下错误:

“未处理的承诺拒绝:模板解析错误: 'child' 不是已知元素:"

我哪里出错了?有人能帮我摆脱痛苦吗?

//app.module.ts
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    ChildModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

//app.component.ts
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  entryComponents: [ChildComponent]
})
export class AppComponent {
  title = 'app works!';
}

  

  <!-- app.component.html -->
    <h1>
      {{title}}
    </h1>
    <child></child>

//child.module.ts
@NgModule({
    declarations:[ChildComponent]
})
export class ChildModule{}

   

 //child.component.ts
    @Component({
        selector:'child',
        templateUrl: './child.component.html'
    })
    export class ChildComponent{
        
    }

   

 <!-- child.component.html -->
    <h1>child</h1>

【问题讨论】:

    标签: angular angular2-directives


    【解决方案1】:

    你必须导出你的组件:

    @NgModule({
        declarations:[ChildComponent],
        exports :[ChildComponent]
    })
    export class ChildModule{}
    

    【讨论】:

    • 非常感谢您的快速回复。那行得通。一旦我被允许,我将标记为已回答。仅供参考,该属性是“出口”而不是“出口”
    猜你喜欢
    • 2017-10-04
    • 2016-07-28
    • 2016-06-13
    • 2016-03-01
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多