5

我将 InversifyJS 与绑定和惰性注入装饰器一起使用,但出现下一个错误:

未找到 serviceIdentifier 的匹配绑定:Symbol(ClassB)

这是代码:

反转配置文件

import { Container } from 'inversify';
import getDecorators from 'inversify-inject-decorators';
import { buildProviderModule, provide } from 'inversify-binding-decorators';
import Types from './types';

const container = new Container();

container.bind<MyType>(Types.MyType)
    .to(MyType);

const { lazyInject } = getDecorators(container, false);

container.load(buildProviderModule());

export { container, lazyInject, provide };

我的类型.ts

import { inject, injectable } from 'inversify';

@injectable()
export class MyType{
    constructor(
        @inject(Q) private readonly Q: Q,
        @inject(W) private readonly W: W,
        @inject(E) private readonly E: E) {
    }
}

Main.ts

import 'reflect-metadata';

import './class-a';
import './class-b';

import { MyType } from './my-type';
import Types from './di/types';
import { container } from './di/inversify.config';


const main = async () => {
    const mytype = container.get<MyType>(Types.MyType);
    await mytype.execute();
};

main()
    .then(() => {
        console.info('Started application...');
    })
    .catch((error) => {
        console.error(error);
    });

类A.ts

import { inject } from 'inversify';
import Types from '../../di/types';
import { provide } from '../../di/inversify.config';
import { ClassB } from '../../some/directory/class-b';

@provide(Types.ClassA)
export class ClassA implements IClassA {
    constructor(
        @inject(ClassX) private readonly ClassX: ClassX,
        @inject(ClassY) private readonly ClassY: ClassY) {
    }

    @lazyInject(ClassB)
    private readonly ClassB!: ClassB;
}
export default ClassA;

B类.ts

import Types from '../../di/types';
import { provide } from '../../di/inversify.config';

@provide(Types.ClassB)
export class ClassB implements IClassB{
...
}
export default ClassB;

类型.ts

const Types = {
    ClassA: Symbol.for('ClassA'),
    ClassB: Symbol.for('ClassB'),
};

export default Types;

我试图按照这个问题的答案 Binding classes with property injection to kernel in inversify 但仍然没有成功

4

0 回答 0