角2错误(“指示”在类型不存在“组件”)

问题描述 投票:9回答:2

我在角2初学者,我用最后的角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
2个回答
16
投票

directives从部件移除。请参阅以下内容:https://stackoverflow.com/a/39410642/5487673

该问题的解决方案是简单地从组件移除directives属性。只要你directives属性下所列的成分以NgModule级声明,那么你应该是正确的。


0
投票

我遇到的情况和解决。作为指令,你不需要导入并将其插入到特定的组件。你可以使用他们喜欢这一点。

首先,导入他们在app.module.ts。二,添加进口指示纳入声明。然后,他们将工作。

© www.soinside.com 2019 - 2024. All rights reserved.