【问题标题】:Unable to use child component in a parent component in Angular2无法在Angular2的父组件中使用子组件
【发布时间】:2017-04-03 11:45:03
【问题描述】:

我正在尝试使用主组件内的子组件,app.component 使用主组件模板内的选择器。但是它不起作用。

child.component.ts

import { Component} from '@angular/core';
@Component({
selector: 'child-component',
templateUrl: `<p>Child Component</p>`
})
export class ChildComponent  {  }

app.component.ts

import { Component } from '@angular/core';
import { ChildComponent} from './child.component';

@Component({
selector: 'my-app',
template: `<h1>Hello Angular!</h1>
<child-component>    </child-component>       `
})

export class AppComponent { }

我在模块中为 ChildComponent 做了如下声明

app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { ChildComponent} from './child.component';
import { AppComponent } from './app.component';

@NgModule({
imports: [BrowserModule],
declarations: [AppComponent,ChildComponent],
bootstrap: [AppComponent]
})

export class AppModule { }

How to import component into another root component in Angular 2 提到的解决方案不起作用。

请帮助解决问题。

【问题讨论】:

    标签: angular components angular2-components


    【解决方案1】:

    您在 child-component 元选项中输入错误。

    templateUrl: `<p>Child Component</p>`
    

    应该是

    template: `<p>Child Component</p>`
    

    【讨论】:

    • 非常感谢。它现在像魔术一样工作。请让我知道如何调试它。我没有成功从终端/控制台跟踪它。
    • 浏览器控制台中的错误消息是解释性的,您应该先查看它们
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-19
    • 2016-07-18
    • 2017-04-22
    • 2016-07-24
    • 1970-01-01
    • 2017-04-21
    相关资源
    最近更新 更多