我正在try 从一个文件导入另一个根组件文件中的组件. 它会给出错误,因为..

区js:484未处理的promise 拒绝:模板分析错误:

My First Angular 2 App

[ERROR ->]"): AppComponent@0:31 ; Zone: ; Task: Promise.then ; Value: Error: Template parse errors:(…) Error: Template parse errors: 'courses' is not a known element:

我的app.Component.ts类似于[根组件文件]

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 { }

还有我的课程.组成部分不要这样;

import { Component } from '@angular/core';
@Component({
  selector: 'courses',
  template: '<h1>courses</h1>'
})
export class CoursesComponent { }

从app.component内的Courses.Component.ts文件导入课程组件时,我无法在@Component{}内声明指令

directives:[CoursesComponent]给我错误

请建议解决方案.

推荐答案

您应该将其声明为100,如下所示(RC5 and later),

import {CoursesComponent} from './courses.component';

@NgModule({
  imports:      [ BrowserModule],
  declarations: [ AppComponent,CoursesComponent],  //<----here
  providers:    [],      
  bootstrap:    [ AppComponent ]
})

Angular相关问答推荐

Angular路由不响应子路由的子路由

有没有一种方法用timeout()操作符创建计数器,用于超时一个观察对象?

为什么我的自定义.d.ts不起作用?LeaderLine不是构造函数

带有服务依赖的Angular测试类

父母和子元素在17号角不保持同步

当包含react 性模板绑定时,NG-Bootstrap手风琴不会打开

RXJS拆分返回值,多次调用后重新加入

无法在单个元素上多次监听同一个事件

具有两个订阅方法的 Angular 16 takeUntilDestroyed 运算符

完成行为主体

res.json() 不是函数

Angular2 SVG xlink:href

错误:formControlName 必须与父 formGroup 指令一起使用.您需要添加一个 formGroup 指令

如何使用 CLI 创建特定版本的 Angular 项目?

unexpected token < 错误

交替行 colored颜色 angular material表

在 Angular 组件模板中添加脚本标签

Angular 2 单元测试 - 出现错误无法加载ng:///DynamicTestModule/module.ngfactory.js

如何在 Angular 5 react式表单输入中使用管道

如何在 Angular CLI 的构建时插入内部版本号或时间戳