首页 文章

无法在Angular2中的父组件中使用子组件

提问于
浏览
2

我正在尝试使用主组件内部的子组件,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中提到的解决方案不起作用 .

请帮忙解决 .

1 回答

  • 1

    你在 child-component 元选项中输了错字 .

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

    应该

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

相关问题