Angular 2组件指令不起作用 [英] Angular 2 component directive not working
问题描述
我是角度2的初学者,我想让我的第一个应用程序正常工作。我正在使用TypeScript。
我有 app.component.ts ,其中我已经向另一个名为 todos.component 的组件发出了指令,但是我在编译时收到以下错误:
I am a beginner in angular 2 and I want to make my first app working. I am using TypeScript. I have the app.component.ts in which I have made a directive to another compoent called todos.component but I am getting the following error at compile time:
[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:";
}
如果没有该指令,该应用程序运行正常。
任何帮助将不胜感激!
Without the directive, the app works fine. Any help would be appreciated!
提前致谢!
推荐答案
在你的<$ c $中c> app.component.ts 您定义指令:[TodosComponent]
。
RC6中的指令属性已从 @Component()
装饰器中删除。
In your app.component.ts
you define directive: [TodosComponent]
.
The directive property has been removed in RC6 from the @Component()
decorator.
解决方案对此,是:
- 创建一个NgModule和
- 在<$中声明TodosComponent c $ c>声明:[] 数组。
- create an NgModule and
- declare the TodosComponent inside the
declarations: []
array.
请参阅此处查看AppModule示例:
See here for an example of AppModule:
https:// angular.io/docs/ts/latest/tutorial/toh-pt3.html
这篇关于Angular 2组件指令不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!