Can';t使用typescript向我的angular 2应用程序添加新组件
我正在使用Angular 2 CLI,并使用Can';t使用typescript向我的angular 2应用程序添加新组件,angular,typescript,atom-editor,angular-components,Angular,Typescript,Atom Editor,Angular Components,我正在使用Angular 2 CLI,并使用ng生成组件MyComponent创建了组件“MyComponent”。据我所知,我必须将组件添加到@componentdecorator的指令键值对中,但typescript编译在这一点上失败,原因是: ERROR in [default] /Users/Philip/Desktop/Angular2/src/app/app.component.ts:8:2 Argument of type '{ selector: string; templat
ng生成组件MyComponent
创建了组件“MyComponent”。据我所知,我必须将组件添加到@component
decorator的指令键值对中,但typescript编译在这一点上失败,原因是:
ERROR in [default] /Users/Philip/Desktop/Angular2/src/app/app.component.ts:8:2
Argument of type '{ selector: string; template: any; styles: any[]; directives: typeof MyComponentComponent[]; }' is not assignable to parameter of type 'Component'.
Object literal may only specify known properties, and 'directives' does not exist in type 'Component'.
这是我的应用程序代码:
import { Component } from '@angular/core';
import { MyComponentComponent } from './my-component/my-component.component'
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
directives: [MyComponentComponent],
})
export class AppComponent {
title = 'app works!';
}
我没有触及生成组件的代码,只是以防万一:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-my-component',
templateUrl: './my-component.component.html',
styleUrls: ['./my-component.component.css']
})
export class MyComponentComponent implements OnInit {
constructor() {
}
ngOnInit() {
}
}
错误本身表明指令不存在于组件中,因为它已被弃用。请尝试下面显示的代码
import { MyComponentComponent } from './my-component/my-component.component'
import {CUSTOM_ELEMENTS_SCHEMA} from '@angular/core';
@NgModule({
...
...
declarations:[AppComponent,MyComponentComponent], //<---need to declare
schemas: [CUSTOM_ELEMENTS_SCHEMA] //<---added this line
})
从“./my component/my component.component”导入{MyComponent}
从“@angular/core”导入{CUSTOM_ELEMENTS_SCHEMA}”;
@NGD模块({
...
...
声明:[AppComponent,MyComponentComponent],//Angular2组件装饰器不再将这些用于嵌入其他组件。我们将需要使用名为entryComponents的新元数据。请参见下面的示例
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
entryComponents:[VehicleListComponent]
})
车辆列表组件具有以下组件元数据
@Component({
selector: 'app-vehicle-list',
templateUrl: './vehicle-list.component.html',
styleUrls: ['./vehicle-list.component.css'],
providers: [VehicleService]
})
如果您使用Angular CLI,则新组件将自动导入并添加到app.module.ts中@ngmodule的声明部分。我们不需要为此编写任何代码。您有两个选项:
1:在组件内部使用entryComponents标记
2:使用Angular CLI自动导入它,因此我们不需要为它显式编写代码。在运行create component命令后,无需对Angular 4或更高版本执行任何操作。错误很可能是您没有从新组件元数据文件(例如componentname.component.ts)中使用正确的选择器名称.您使用的是angular2的哪个版本?您使用的CLI版本是否与您使用的angular版本相匹配?指令在组件级别被弃用,然后被删除(在RC6中,IIRC).我今天使用文档中所示的npm命令安装了这两个。它们都应该是最新的,但我不知道它们是否匹配。您是否使用了angular-cli@webpack
?谢谢。我已经在app.module.ts中声明了这一点,如CLI在您的帖子中所示。typescript可以编译,但不会在浏览器中运行。它是一个巨大的错误,我已经将它更新到我的原始帖子。然后你从@Component
>中删除了指令:[……]
部分吗?只需删除它。我希望它会开始工作……我不确定,但我只是用模式更新了我的答案。现在检查它是否工作。有什么问题吗?@amirhossein你需要像导入导入{NgModule}一样导入它从“@angular/core”;
我刚刚用“entryComponents”替换了“directives”并使它开始工作。在这里阅读更多内容:这里的问题相同,这个答案很有帮助。有人能解释一下名称更改的原因吗?我还参考了@NgModule将组件放在声明键上并导入到文件顶部的内容(在我的例子中,@NgModule在app.module.ts上)。