我是角度2的初學者,我想讓我的第一個應用程序工作。我正在使用TypeScript。 我有app.component.ts中,我已經做了一個指令,稱爲todos.component但我在編譯時得到以下錯誤另一compoent:Angular 2組件指令不起作用
[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.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:";
}
沒有指令,應用程序工作正常。 任何幫助,將不勝感激!
在此先感謝!
你不是以錯誤的方式創建一個指令嗎?來自'@ angular/core'的Angular2文檔'import {Directive,ElementRef,Input,Renderer}; @Directive({selector:'[myHighlight]'}) export class HighlightDirective {elt:ElementRef,renderer:Renderer){ renderer.setElementStyle(el.nativeElement,'backgroundColor','yellow'); } } ' – Pradeepb
如果您使用的是最新的Angular final,'@ Component'元數據中不存在'directives'(因此錯誤)。如果你正在做一個教程,你應該嘗試找到一個更新的教程。在短時間內發生了很多變化,很多教程已經過時。我個人只是去[Angular文檔](https://angular.io/docs/ts/latest/)並從那裏開始。 –
同意@peeskillet,看看這個https://angular.io/docs/ts/latest/guide/attribute-directives.html。不要使用@Component的'directives:',只將它添加到'app.module.ts'的'聲明:'中。 – haxpor