本文介绍了如何在Angular 2中将组件导入另一个根组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从一个文件导入另一个根组件文件.它给出错误为..
I am trying to import component from one file another root component file.it give error as ..
我的app.component.ts像[根组件文件]
My app.component.ts be like [root component file]
import { Component } from '@angular/core';
import {CoursesComponent} from './courses.component';
@Component({
selector: 'my-app',
template: '<h1>My First Angular 2 App</h1><courses></courses>',
directives:[CoursesComponent],
})
export class AppComponent { }
和我的course.component.ts是;
and my courses.component.ts be ;
import { Component } from '@angular/core';
@Component({
selector: 'courses',
template: '<h1>courses</h1>'
})
export class CoursesComponent { }
从app.component内的courses.component.ts文件导入课程组件时,我无法在@Component{}
while importing courses component from courses.component.ts file inside app.component i am not able declare directive inside @Component{}
directives:[CoursesComponent]
给我错误
请提出解决方案.
推荐答案
您应使用 declarations array(meta property) of @NgModule
进行声明,如下所示(RC5 and later
),
import {CoursesComponent} from './courses.component';
@NgModule({
imports: [ BrowserModule],
declarations: [ AppComponent,CoursesComponent], //<----here
providers: [],
bootstrap: [ AppComponent ]
})
这篇关于如何在Angular 2中将组件导入另一个根组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!