2016-11-10 60 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中。其次,將導入的指令添加到聲明中。 然後他們會工作。

相關問題