2016-09-23 31 views
22

Tôi là người mới bắt đầu ở góc 2 và tôi muốn làm cho ứng dụng đầu tiên của mình hoạt động. Tôi đang sử dụng TypeScript. Tôi có app.component.ts trong đó tôi đã thực hiện một chỉ thị khác compoent gọi todos.component nhưng tôi nhận được lỗi sau tại thời gian biên dịch:Chỉ thị 2 thành phần góc không hoạt động

[0] app/app.component.ts(7,3): error TS2345: Argument of type '{ moduleId: string; selector: string; directives: typeof TodosComponent[]; templateUrl: string; s ...' is not assignable to parameter of type 'Component'. 
[0] Object literal may only specify known properties, and 'directives' does not exist in type 'Component'. 

Mã của tôi là như thế này :

index.html

<html> 
    <head> 
    <title>Angular 2 QuickStart</title> 
    <meta charset="UTF-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="styles.css"> 
    <!-- 1. Load libraries --> 
    <!-- Polyfill(s) for older browsers --> 
    <script src="node_modules/core-js/client/shim.min.js"></script> 
    <script src="node_modules/zone.js/dist/zone.js"></script> 
    <script src="node_modules/reflect-metadata/Reflect.js"></script> 
    <script src="node_modules/systemjs/dist/system.src.js"></script> 
    <!-- 2. Configure SystemJS --> 
    <script src="systemjs.config.js"></script> 
    <script> 
     System.import('app').catch(function(err){ console.error(err); }); 
    </script> 
    </head> 
    <!-- 3. Display the application --> 
    <body> 
    <app-root>Loading...</app-root> 
    </body> 
</html> 

app.componen t.ts

import { Component } from '@angular/core'; 
import {TodosComponent} from './todos/todos.component'; 

@Component({ 
    moduleId : module.id, 
    selector: 'app-root', 
    directives: [TodosComponent], 
    templateUrl : 'app.component.html', 
    styleUrls : ['app.component.css'] 
}) 

export class AppComponent { 
    title: string = "Does it work?"; 
} 

app.component.html:

<h1> Angular 2 application</h1> 
{{title}} 
<app-todos></app-todos> 

todos.component.ts

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

@Component({ 
    moduleId : module.id, 
    selector: 'app-todos', 
    template: '<h2>Todo List</h2>' 
}) 

export class TodosComponent { 
    title: string = "You have to do the following today:";  
} 

Nếu không có chỉ thị, ứng dụng hoạt động tốt. Bất kỳ trợ giúp nào sẽ được đánh giá cao!

Cảm ơn trước!

+0

Bạn không tạo chỉ thị sai hướng sao? từ Tài liệu Angular2 'nhập khẩu {Chỉ thị, ElementRef, Đầu vào, Trình kết xuất} từ 'góc/lõi của' @; @Directive ({selector: '[myHighlight]'}) lớp xuất bản HighlightDirective { hàm tạo (el: ElementRef, trình kết xuất: Trình kết xuất) { renderer.setElementStyle (el.nativeElement, 'backgroundColor', 'yellow'); } } ' – Pradeepb

+2

Nếu bạn đang sử dụng phiên bản Final Angular mới nhất,' chỉ thị' không tồn tại trong siêu dữ liệu '@ Component' nữa (do đó là lỗi). Nếu bạn đang đi ra một hướng dẫn, bạn nên cố gắng tìm một hướng dẫn mới hơn. Đã có rất nhiều thay đổi trong một khoảng thời gian ngắn và rất nhiều hướng dẫn đã lỗi thời. Cá nhân tôi chỉ cần vào [Tài liệu góc] (https://angular.io/docs/ts/latest/) và bắt đầu từ đó. –

+0

Đồng ý với @peeskillet, hãy xem https://angular.io/docs/ts/latest/guide/attribute-directives.html này. Chỉ cần không sử dụng 'chỉ thị:' của @Component, chỉ thêm nó vào 'khai báo' của 'app.module.ts'. – haxpor

Trả lời

50

Trong số app.component.ts bạn xác định directive: [TodosComponent]. Thuộc tính chỉ thị đã được xóa trong RC6 từ trang trí @Component().

Các giải pháp này, là để:

  1. tạo NgModule và
  2. bố TodosComponent bên trong mảng declarations: [].

Xem ở đây cho một ví dụ về AppModule:

https://angular.io/docs/ts/latest/tutorial/toh-pt3.html

+0

Cảm ơn! Đó là nó! – candino

+2

@Alexander Nếu tôi muốn tải chỉ thị cho một thành phần cụ thể không giống như toàn bộ ứng dụng thì sao? FYI - Tôi đang cố tải trình soạn thảo văn bản tinymce. –

+1

Bạn nên tạo một mô-đun ra khỏi thành phần đó và khai báo cả thành phần và chỉ thị tại đó. –

3

module.id đã được bổ sung trong intially khi angular2 đang ở phiên bản beta version.Since với phiên bản mới và cli góc hỗ trợ nó không được yêu cầu để thêm moduleId: module.id, bạn có thể xóa khỏi tệp .ts

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