Angular 2 - 'ComponentMetadataType'类型中不存在'进口'

时间:2016-08-19 10:55:02

标签: javascript angularjs angular typescript

我有一个Angular 2应用程序,现在我想使用双向绑定。当我尝试分配imports: [FormsModule]时,发生了错误:

Argument of type '{ imports: typeof F...' is not assignable to parameter of type 'ComponentMetadataType'.
Object literal may only specify known properties, and 'imports' does not exist in type 'ComponentMetadataType'.

以下是代码:

import {Component, OnInit} from '@angular/core';
import  {TodoService} from '../todo.service'
import {FormsModule} from '@angular/forms'

@Component({
  moduleId: module.id,
  selector: 'app-todos',
  templateUrl: 'todos.component.html',
  styleUrls: ['todos.component.css'],
  imports: [FormsModule]
})

1 个答案:

答案 0 :(得分:2)

您无法在组件中使用imports。您应该在ngModule装饰器中导入所有必需的依赖项。

请查看Angular JS documentation的以下示例:

import { NgModule }      from '@angular/core';
import { FormsModule }   from '@angular/forms';
import { AppComponent }  from './app.component';

@NgModule({
  imports: [
    FormsModule
  ],
  declarations: [
    AppComponent
  ],
  bootstrap: [ AppComponent ]
})

export class AppModule { }