javascript - Angular 2 - 'imports' 不存在于类型 'ComponentMetadataType' 中

标签 javascript angularjs angular typescript

我有一个 Angular 2 应用程序,现在我想使用双向绑定(bind)。当我尝试分配 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]
})

最佳答案

您不能在组件中使用导入。您应该在 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 { }

关于javascript - Angular 2 - 'imports' 不存在于类型 'ComponentMetadataType' 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39037271/

相关文章:

Angular 观察者模拟 Observable<string>

javascript - "privileged"方法调用成员方法问题

javascript - 单击任何按钮都会触发我的 UserService.getUsers 函数,但这不应该发生

javascript - 输入迄今为止搜索的术语时,jQuery UI 自动完成不会触发搜索

javascript - Angular : Passing data back to my controller from a factory ajax call

javascript - 在 ng-repeat 之外检索 Angular ng-repeat 范围

javascript - Bootstrap 折叠类无法正确切换

angularjs - 如何使用angularjs设置下拉默认值

android - 添加 nativescript-ui-dataform 插件后 NativeScript tns 构建失败

javascript - 利用 Angular 中的导入函数进行插值