无法添加一个新的组件到我的角度2应用程序与typescript
发布时间:2020-12-17 07:40:39 所属栏目:安全 来源:网络整理
导读:我使用Angular 2 CLI,我用ng生成组件MyComponent创建了组件“MyComponent”.据我所知,我必须将组件添加到@Component装饰器的指令键值对,但是在这一点上,typescript编译失败,说: ERROR in [default] /Users/Philip/Desktop/Angular2/src/app/app.component.t
我使用Angular 2 CLI,我用ng生成组件MyComponent创建了组件“MyComponent”.据我所知,我必须将组件添加到@Component装饰器的指令键值对,但是在这一点上,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() { } } 我试图用atom中的“atom-typescript”插件来编译这个scriptcript.
错误本身表示组件中不存在伪指令,因为它已被弃用.请尝试下面的代码, 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 }) 并从AppComponent中删除指令:[MyComponentComponent]. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |