جافا سكريبت - توجيه مكون Angular 2 لا يعمل


أنا مبتدئ في Angular 2 وأريد جعل تطبيقي الأول يعمل. أنا أستخدم TypeScript. لدي ملف app.component.ts الذي قمت فيه بعمل توجيه لمكون آخر يسمى todos.component لكنني أحصل على الخطأ التالي عند وقت الترجمة:

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

الشفرة الخاصة بي هكذا:

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.component.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:";    
}

بدون التوجيه، يعمل التطبيق بشكل جيد. أي مساعدة ستكون موضع تقدير!

شكراً مقدماً!

الإجابة رقم 1

في ملف app.component.ts الخاص بك، تقوم بتعريف directive: [TodosComponent]. تم إزالة خاصية directive في RC6 من ديكوريتر @Component().

الحل لهذا هو:

  1. إنشاء NgModule و
  2. قم بإعلان TodosComponent داخل مصفوفة declarations: [].

انظر هنا لمثال عن AppModule:

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

الإجابة رقم 2

تمت إضافة module.id في البداية عندما كانت نسخة Angular2 في الإصدار التجريبي. نظرًا للإصدار الجديد ودعم Angular CLI، لم يعد من الضروري إضافة moduleId:module.id، يمكنك إزالته من ملفات .ts