我使用Angular 2 CLI,我用ng生成组件MyComponent创建了组件“MyComponent”.据我所知,我必须将组件添加到@Component装饰器的指令键值对,但是在这一点上,typescript编译失败,说:
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() { } }
我试图用atom中的“atom-typescript”插件来编译这个scriptcript.
错误本身表示组件中不存在伪指令,因为它已被弃用.请尝试下面的代码,
import { MyComponentComponent } from './my-component/my-component.component' import {CUSTOM_ELEMENTS_SCHEMA} from '@angular/core'; @NgModule({ ... ... declarations:[AppComponent,MyComponentComponent],//<---need to declare schemas: [CUSTOM_ELEMENTS_SCHEMA] //<---added this line })
并从AppComponent中删除指令:[MyComponentComponent].