【问题标题】:Angular 2 Error ( 'directives' does not exist in type 'Component' )Angular 2 错误(“组件”类型中不存在“指令”)
【发布时间】:2017-03-25 03:10:41
【问题描述】:

我是 Angular 2 的初学者,我正在使用最终的 Angular 2 发布版本。我有一个奇怪的问题。 这是我的 databinding.component.ts 代码:

import { Component } from '@angular/core';

import {PropertyBindingComponent} from './property-binding.component';
import {EventBindingComponent} from './event-binding.component';


@Component({
  selector: 'fa-databinding',
  templateUrl: 'databinding.component.html',
  styleUrls: ['databinding.component.css'],
  directives: [PropertyBindingComponent, EventBindingComponent]
})

这是我的 app.module.ts 代码:

import { PropertyBindingComponent } from './databinding/property-binding.component';
import { EventBindingComponent } from './databinding/event-binding.component';

@NgModule({
  declarations: [
    AppComponent,
    OtherComponent,
    AnotherComponent,
    DatabindingComponent,
    PropertyBindingComponent,
    EventBindingComponent
  ]

此代码无法正常工作:

ERROR in [default] /home/tornado/work/first-app/src/app/databinding/databinding.component.ts:11:2 
Argument of type '{ selector: string; template: any; styles: any[]; directives: (typeof PropertyBindingComponent | ...' is not assignable to parameter of type 'Component'.
  Object literal may only specify known properties, and 'directives' does not exist in type 'Component'.

我该怎么办?!?!

【问题讨论】:

标签: angular typescript data-binding components


【解决方案1】:

directives 已从组件中删除。 请参阅以下内容:https://stackoverflow.com/a/39410642/5487673

解决这个问题的方法很简单,就是从你的组件中移除directives 属性。只要您的directives 属性下列出的组件在NgModule 级别声明,那么您应该是对的。

【讨论】:

  • 如果您觉得这个答案有帮助,请点击 ✓ 勾选按钮接受它。
  • 我也有同样的“绑定”,也许可以提供替代方案,而不是让我们空手而归。
  • 别无选择,您只需从组件中删除“指令”即可。只要您在模块中声明了“指令”组件,那么您就可以开始了。不过更新了答案。
【解决方案2】:

我遇到了那个案例并解决了。 作为指令,您不需要将它们导入并插入特定组件。 你可以这样使用它们。

首先,将它们导入 app.module.ts。 其次,将导入的指令添加到声明中。 然后他们就可以工作了。

【讨论】:

    猜你喜欢
    • 2017-02-02
    • 2017-06-11
    • 2016-07-09
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    相关资源
    最近更新 更多