需要与Inversify相关的实施方面的帮助。我正在创建一个从节点扩展EventEmitter的类。当我尝试使用反转时,它说EventEmitter不可注射。以下是示例代码
//接口
export interface ISubscriber {
Connect(callback: Function);
on(event: string, listener: Function): this;
emit(event: string, ...args: any[]): boolean;
}
//类
import {EventEmitter} from 'events';
@injectable()
class Subscriber extends EventEmitter implements ISubscriber {
logProvider: SCLogging.ILogger;
public constructor(
@inject(TYPES.ILogger) logProvider: SCLogging.ILogger,
@inject(TYPES.IConfig) config: IConfig
) {
super();
//Some Implementation
}
public Connect(callback) {
//Some Implementation
}
public on(event: string, listener: Function): this {
super.on(event, listener);
return this;
}
public emit(event: string, ...args: any[]): boolean {
return super.emit(event, ...args);
}
}
export { ISubscriber, Subscriber }
//定义绑定
kernel.bind<SCLogging.ILogger>(TYPES.ILogger).to(Logger);
kernel.bind<IConfig>(TYPES.IConfig).to(Config);
kernel.bind<ISubscriber>(TYPES.ISubscriber).to(Subscriber);
我收到错误
Error: Missing required @injectable annotation in: EventEmitter.
答案 0 :(得分:5)
一个非常类似的问题已经answered on the InversifyJS issues on Github:
您可以使用decorate函数调用装饰器:
import { decorate, injectable } from "inversify"; decorate(injectable(), ClassName)
查看https://github.com/inversify/InversifyJS/blob/master/wiki/basic_js_example.md了解详情。
请参阅Github上的问题以获取更多信息。
答案 1 :(得分:1)
在容器选项中设置skipBaseClassChecks: true
会禁用逆转的“功能”。
有关更多详细信息,请参见此PR https://github.com/inversify/InversifyJS/pull/841