Angular2 RC6升级

时间:2016-09-02 12:40:56

标签: angular angular2-upgrade

将我的项目更新为RC6后发生错误:

Error: Typescript found the following errors:
  app.component.ts (12, 3): Argument of type '{ moduleId: string; selector: string; templateUrl: string; styleUrls: string[]; directives: (type...' is not assignable to parameter of type 'ComponentMetadataType'.
  Object literal may only specify known properties, and 'directives' does not exist in type 'ComponentMetadataType'.

app.component.ts

import {Component, OnInit} from '@angular/core';
import {NavbarComponent} from "./shared/navbar/navbar.component";
import {ROUTER_DIRECTIVES} from "@angular/router";
import {SidebarComponent} from "./shared/sidebar/sidebar.component";
import {FooterComponent} from "./shared/footer/footer.component";

@Component({
  moduleId: module.id,
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.css'],
  directives: [NavbarComponent, SidebarComponent, FooterComponent, ROUTER_DIRECTIVES]
})
export class AppComponent implements OnInit {

  ngOnInit(): any {
    return undefined;
  }

}

我需要一段时间,但我无法弄明白。

3 个答案:

答案 0 :(得分:17)

必须在Directives中定义

pipes@NgModule如果我正确阅读。 <{1}}内的支持已被删除。

所以,只需将你的指令移到NgModule

目前你有:组件A带有指令Ac,组件B带有指令Bc,很可能是一个AppModule,你只需要将Ac和Bc移动到AppModule中。是的,你必须从@Component声明

中删除它们

然后,指令将立即在模块中定义的组件中可用。

OP的例子变为:

app.component.ts

@Component

app.module.ts

// imports...
@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
})
export class AppComponent {}

请参阅路由器文档router documentation

答案 1 :(得分:5)

自RC6以来,必须在directives声明中定义

pipes@NgModule。 从@Component装饰器中删除它们。

答案 2 :(得分:2)

对于Angular 2最终版本2.0.0.0

管道应该在app.module.ts文件的声明部分声明

import {KeysPipe} from './keys.pipe';

@NgModule({
  imports:      [ BrowserModule, FormsModule, HttpModule ],
  declarations: [ AppComponent, LoginComponent,ArticleComponent,**KeysPipe** ],
  bootstrap:    [ AppComponent, LoginComponent,ArticleComponent ],


})

只需观察上面代码段中的键盘实现。

相关问题