【发布时间】:2017-01-23 21:01:40
【问题描述】:
我正在使用 Angular 2 CLI,并使用 ng generate component MyComponent 创建了组件“MyComponent”。据我所知,我必须将组件添加到 @Component 装饰器的指令键值对中,但是此时打字稿编译失败,说:
ERROR in [default] /Users/Philip/Desktop/Angular2/src/app/app.component.ts:8:2
Argument of type '{ selector: string; template: any; styles: any[]; directives: typeof MyComponentComponent[]; }' is not assignable to parameter of type 'Component'.
Object literal may only specify known properties, and 'directives' does not exist in type 'Component'.
这是我的应用代码:
import { Component } from '@angular/core';
import { MyComponentComponent } from './my-component/my-component.component'
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
directives: [MyComponentComponent],
})
export class AppComponent {
title = 'app works!';
}
生成组件的代码我没有碰,只是以防万一:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-my-component',
templateUrl: './my-component.component.html',
styleUrls: ['./my-component.component.css']
})
export class MyComponentComponent implements OnInit {
constructor() {
}
ngOnInit() {
}
}
【问题讨论】:
-
你用的是哪个版本的angular2?
-
您使用的 CLI 版本是否与您正在使用的 Angular 版本相匹配?组件级别的
directives已被弃用,然后被删除(在 RC6、IIRC 中)。 -
我今天使用 npm 命令安装了这两个,如文档中所示。两者都应该是最新的,但我不知道它们是否匹配。
-
你用过
angular-cli@webpack吗?
标签: angular typescript atom-editor angular-components