typescript Angular 2 错误(“组件”类型中不存在“指令”)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40538634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:00:16  来源:igfitidea点击:

Angular 2 Error ( 'directives' does not exist in type 'Component' )

angulartypescriptdata-bindingcomponents

提问by Mahdi Ta'ala

I am beginner in Angular 2 and I'm using the final Angular 2 release version. I have a strange problem with it. this is my databinding.component.tscode:

我是 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]
})

and this is a peace of my app.module.tscode :

这是我的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
  ]

This code does not work correctly:

此代码无法正常工作:

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'.

What should I do?!?!

我该怎么办?!?!

回答by Simon K

directiveswas removed from component. See the following: https://stackoverflow.com/a/39410642/5487673

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

The solution to the problem is simply to remove the directivesattribute from your component. So long as the components listed under your directivesattribute are declared at the NgModulelevel then you should be right.

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

回答by smartworld-dm

I encountered that case and solved. As directives, you don't need to import and plug them to specific component. You could use them like this.

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

First, import them on the app.module.ts. Second, add imported directive into declarations. Then they would be working.

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