2016-11-10 92 views
5

我是Angular 2的初学者,我正在使用最终的Angular 2发布版本。我有一个奇怪的问题。 这是我databinding.component.ts代码:Angular 2错误('指令'在'Component'类型中不存在)

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

import {PropertyBindingComponent} from './property-binding.component'; 
import {EventBindingComponent} from './event-binding.component'; 


@Component({ 
    selector: 'fa-databinding', 
    templateUrl: 'databinding.component.html', 
    styleUrls: ['databinding.component.css'], 
    directives: [PropertyBindingComponent, EventBindingComponent] 
}) 

,这是我app.module.ts的和平代码:

import { PropertyBindingComponent } from './databinding/property-binding.component'; 
import { EventBindingComponent } from './databinding/event-binding.component'; 

@NgModule({ 
    declarations: [ 
    AppComponent, 
    OtherComponent, 
    AnotherComponent, 
    DatabindingComponent, 
    PropertyBindingComponent, 
    EventBindingComponent 
    ] 

此代码不能正常工作:

ERROR in [default] /home/tornado/work/first-app/src/app/databinding/databinding.component.ts:11:2 
Argument of type '{ selector: string; template: any; styles: any[]; directives: (typeof PropertyBindingComponent | ...' is not assignable to parameter of type 'Component'. 
    Object literal may only specify known properties, and 'directives' does not exist in type 'Component'. 

我该怎么办?!?!

+0

[Angular2版本RC.6“指令”里面的@Component错误](http://stackoverflow.com/questions/39410591/angular2-version-rc-6-directives-inside-component-error) – Fiddles

回答

12

directives已从组件中移除。 参见以下内容:https://stackoverflow.com/a/39410642/5487673

解决该问题的方法很简单,就是从组件中删除directives属性。只要您的directives属性下列出的组件声明在NgModule级别,那么您应该是对的。

+1

谢谢西蒙:-) –

+1

如果您觉得此答案有帮助,请点击✓勾号按钮。 –

+0

我对'绑定'也一样,也许提供了替代方案,而不是让我们空手而归。 – tatsu

0

我遇到过这种情况并解决了。 作为指令,您不需要导入并将它们插入特定组件。 你可以像这样使用它们。

首先,将它们导入到app.module.ts中。其次,将导入的指令添加到声明中。 然后他们会工作。

相关问题