2016-08-28 59 views
2

我有一些组件(例如评论)添加到我的应用程序: 我想:单独的组件添加到角2

在comment.component.ts

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

@Component({ 
    selector: 'am-comments', 
    template: `<h4>{{title}}</h4>` 
}) 

export class CommentComponent { 
    title = 'Comments title'; 
} 

我的应用程序.module.ts:

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

import { AppComponent } from './app.component'; 
import { CommentComponent } from './comment.component'; <-- 

@NgModule({ 
    imports: [ 
     BrowserModule, 
     FormsModule 
    ], 
    declarations: [ 
     AppComponent, 
     CommentComponent // <-- 
    ], 
    bootstrap: [ AppComponent ] 
}) 
export class AppModule { } 

最后在输出:

<am-comments>Waiting...</am-comments> 

但它不从CommentComponent outsite父组件输出模板

我错过了什么?

风向结构很简单:

enter image description here

+0

您需要将包含'CommentComponent'的模块导入到您使用它的模块中。在'AppModule'或其他模块中,'正在等待......吗? –

+0

我需要创建单独的组件,但使用这种方法我只能创建子组件。 例如在父母组件模板中:template:'

somehtml

这里将会是子注释组件' – WebArtisan

+0

对不起,我不理解您的评论。你可以尝试重新措辞吗? “只能创建子组件”的意思是什么,你是什么意思,“不从父组件外的CommentComponent输出模板”? –

回答

1

Angular2部件和指令不能直接使用的根组件之外。它们只能在另一个组件的模板中使用。

唯一的方法是引导不同的模块作为附加的Angular2应用程序,其中每个引导模块具有为bootstrap: [ XxxComponent ]注册的不同组件。

指令和管道根本无法在根组件之外使用。

当前,每个引导模块的根组件需要有不同的选择器。这是计划在未来改变。