Javascript 角度2组件指令不工作
我是angular 2的初学者,我想让我的第一个应用程序运行起来。我正在使用打字脚本。 我有一个app.component.ts,其中我对另一个名为todos.component的组件发出了一个指令,但在编译时我遇到了以下错误:Javascript 角度2组件指令不工作,javascript,angular,typescript,Javascript,Angular,Typescript,我是angular 2的初学者,我想让我的第一个应用程序运行起来。我正在使用打字脚本。 我有一个app.component.ts,其中我对另一个名为todos.component的组件发出了一个指令,但在编译时我遇到了以下错误: [0] app/app.component.ts(7,3): error TS2345: Argument of type '{ moduleId: string; selector: string; directives: typeof TodosComponent[
[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.html:
<h1> Angular 2 application</h1>
{{title}}
<app-todos></app-todos>
Angular 2应用程序
{{title}}
todos.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?";
}
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'导入{Component,OnInit};
@组成部分({
moduleId:module.id,
选择器:“应用程序待办事项”,
模板:“待办事项列表”
})
将类导出到组件{
title:string=“您今天必须执行以下操作:”;
}
如果没有该指令,应用程序工作正常。
任何帮助都将不胜感激
提前谢谢 在您的
app.component.ts
中定义指令:[ToDoComponent]
。
RC6中的指令属性已从@Component()
装饰器中删除
解决办法是:
声明:[]
数组中声明TODOComponentmodule.id最初是在angular2处于beta版本时添加的。由于使用新版本和angular cli支持,因此不需要添加moduleId:module.id,您可以从.ts文件中删除您是否以错误的方式创建了指令?来自Angular2文档
import{Directive,ElementRef,Input,Renderer}来自“@angular/core”@指令({selector:'[myHighlight]})导出类HighlightDirective{constructor(el:ElementRef,renderer:renderer){renderer.setElementStyle(el.nativeElement,'backgroundColor','yellow');}
如果您使用的是最新的Angular final,指令
在@Component
元数据中不再存在(因此出现了错误)。如果你要离开一个教程,你应该试着找一个更新的。在短时间内有很多变化,而且很多教程都过时了。我个人会从那里开始。同意@peeskillet,看看这个。只是不要使用@Component的指令:
,只把它添加到app.mod中ule.ts
's声明:
@Alexander如果我想为一个特定的组件加载一个指令而不是整个应用程序,那该怎么办?仅供参考-我试图加载tinymce文本编辑器。你应该用单个组件制作一个模块,并在那里声明组件和指令。@Alexandercieski我发现了这个“错误”很容易做到。和你提到的概念一样。谢谢