typescript “类型‘EventEmitter’不是通用的”角度错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49911879/
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
"Type 'EventEmitter' is not generic" ERROR in angular
提问by thegreathypocrite
I'm currently following a tutorial and the tutorial is making use of EventEmitter
. The code goes like this
我目前正在学习一个教程,该教程正在使用EventEmitter
. 代码是这样的
@Output() ratingClicked: EventEmitter<string> =
new EventEmitter<string>();
But it visual studio code gives me these errors:
但它的 Visual Studio 代码给了我这些错误:
- Type 'EventEmitter' is not generic.
- Expected 0 type arguments, but got 1.
- 'EventEmitter' 类型不是通用的。
- 预期 0 个类型参数,但得到 1 个。
Even in the angular websiteit looks like that code is correct.
即使在angular 网站中,该代码看起来也是正确的。
I'm currently using Angular CLI: 1.7.4; Node: 8.11.1; Typescript: 2.8.1
我目前正在使用 Angular CLI:1.7.4;节点:8.11.1;打字稿:2.8.1
回答by basarat
You are probably using the node
native EventEmitter from node/index.d.ts
i.e.
您可能正在使用node
来自node/index.d.ts
ie的本地 EventEmitter
import { EventEmitter } from 'events';
Fix
使固定
Change the import to the one from angular:
将导入更改为从 angular 导入:
import { EventEmitter } from '@angular/core';
回答by Nipuna
When using Visual Studio Code, The IDE automatically imports EventEmitter from Node.js.
使用 Visual Studio Code 时,IDE 会自动从 Node.js 导入 EventEmitter。
import { EventEmitter } from "events";
In this case you'll have to manually change it to angular core module
在这种情况下,您必须手动将其更改为 angular core 模块
import { EventEmitter } from "@angular/core";
回答by trustidkid
In visual studio code when your try to listen to user click event from your html file of the component
在 Visual Studio 代码中,当您尝试从组件的 html 文件中侦听用户单击事件时
@Output() event: EventEmitter<string> = new EventEmitter<string>();
@Output() event: EventEmitter<string> = new EventEmitter<string>();
it automatically import this to the component import { EventEmitter } from '@angular/event'
instead of import { EventEmitter } from '@angular/core'
.
它会自动将其导入组件import { EventEmitter } from '@angular/event'
而不是import { EventEmitter } from '@angular/core'
.
Resource: https://ultimatecourses.com/blog/component-events-event-emitter-output-angular-2
资源:https: //ultimatecourses.com/blog/component-events-event-emitter-output-angular-2
回答by Mumin Quadri
I was doing the same tutorial and faced the same issue.
我正在做同样的教程并面临同样的问题。
It is a problem with an import. EventEmitter
must be imported from @angular/core
这是导入的问题。EventEmitter
必须从@angular/core
Use:
利用:
import { EventEmitter } from '@angular/core';
This will fix it.
这将修复它。