Angular 2 - 'imports' does not exist in type 'Co

Angular 2 - 'imports' does not exist in type 'ComponentMetadataType'

本文关键字:exist in Co type not imports Angular does      更新时间:2023-09-26

我有一个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]
})

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

请查看Angular JS文档中的以下示例:

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 { }