2017-01-15 70 views
0

我学到角2,但我不能做一个多组分,用于为例我试试这个,是doesen't工作:角2 - 多个组件无法正常工作

app.module.ts:

import { NgModule }  from '@angular/core'; 
import { BrowserModule } from '@angular/platform-browser'; 

import { AppComponent } from './app.component'; 
import { TestComponent } from './test.component'; 

@NgModule({ 
    imports:  [ BrowserModule ], 
    declarations: [ AppComponent, TestComponent ], 
    bootstrap: [ AppComponent ] 
}) 
export class AppModule { } 

app.component.ts:

import { Component } from '@angular/core'; 

@Component({ 
    moduleId: module.id, 
    selector: 'my-app', 
    templateUrl: './app.component.html' 
}) 

export class AppComponent {} 

test.component.ts:

import { Component } from '@angular/core'; 

@Component({ 
    moduleId: module.id, 
    selector: 'test', 
    templateUrl: './test.component.html' 
}) 

export class TestComponent {} 

app.component.html:

<h1>Hello World!</h1> 

test.component.html:

<h2>test</h2> 
+2

你能告诉我们,你在'./ app.component.html'中做了什么吗?如果你已经在'AppComponent' html中指定'test',它就会工作。 –

+3

你想让你的测试组件在你的应用程序组件中显示吗?如果是这样,请将''添加到'app.component.html' –

回答

1

你要 “调用” 从应用测试组件。

而在Angular 2中,它使用html标签选择器完成。

如果您在app.component.html添加<test></test>

,它应该呈现的测试组件。