2016-11-10 41 views
5

Tôi là người mới bắt đầu trong Angular 2 và tôi đang sử dụng phiên bản phát hành Angular 2 cuối cùng. Tôi có một vấn đề lạ với nó. đây là databinding.component.ts tôi mã:Lỗi góc 2 ('chỉ thị' không tồn tại trong loại 'Hợp phần')

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] 
}) 

và đây là một sự yên app.module.ts tôi mã:

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

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

Mã này không hoạt động chính xác:

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'. 

Tôi nên làm gì?!?!

+0

Bản sao có thể có của chỉ thị [Angular2 phiên bản RC.6 "bên trong @Component Error] (http://stackoverflow.com/questions/39410591/angular2-version-rc-6-directives-inside-component-error) – Fiddles

Trả lời

12

directives đã bị xóa khỏi thành phần. Xem các mục sau: https://stackoverflow.com/a/39410642/5487673

Giải pháp cho vấn đề chỉ đơn giản là xóa thuộc tính directives khỏi thành phần của bạn. Miễn là các thành phần được liệt kê trong thuộc tính directives của bạn được khai báo ở mức NgModule thì bạn nên đúng.

+1

Cảm ơn bạn, Simon :-) –

+1

Nếu bạn thấy câu trả lời này hữu ích chấp nhận nó bằng cách nhấp vào nút ✓ đánh dấu. –

+0

Tôi có cùng 'ràng buộc', có thể cung cấp giải pháp thay thế thay vì để chúng tôi tay trống. – tatsu

0

Tôi đã gặp phải trường hợp đó và được giải quyết. Là chỉ thị, bạn không cần nhập và cắm chúng vào thành phần cụ thể. Bạn có thể sử dụng chúng như thế này.

Trước tiên, nhập chúng trên app.module.ts. Thứ hai, thêm chỉ thị đã nhập vào các khai báo. Sau đó, họ sẽ làm việc.

Các vấn đề liên quan