admin管理员组文章数量:1389928
I am using inversifyJs for DI in my typescript project. When using the decorator @multiInject, I am getting the error "Ambiguous match found for serviceIdentifier". I am following this example (.md). Why am I getting this error? Any help will be appreciated. Thanks.
import 'reflect-metadata';
import { Container, multiInject, injectable, } from 'inversify';
interface IWeapon {
name: string;
}
interface INinja {
weapons: IWeapon[],
displayWeapons(): void,
}
@injectable()
class Katana implements IWeapon {
public name = 'Katana';
}
@injectable()
class Shuriken implements IWeapon {
public name = 'Shuriken';
}
@injectable()
class Ninja implements INinja {
public weapons: IWeapon[];
constructor(
@multiInject('Weapon') _weapons: IWeapon[],
) {
this.weapons = _weapons;
}
public displayWeapons = () => {
console.log(this.weapons[0].name, this.weapons[1].name);
}
}
const container = new Container();
container.bind<INinja>("Ninja").to(Ninja);
container.bind<IWeapon>("Weapon").to(Katana);
container.bind<IWeapon>("Weapon").to(Shuriken);
const ninja = container.get<INinja>('Weapon');
ninja.displayWeapons(); // Should display all weapons.
I am using inversifyJs for DI in my typescript project. When using the decorator @multiInject, I am getting the error "Ambiguous match found for serviceIdentifier". I am following this example (https://github./inversify/InversifyJS/blob/master/wiki/multi_injection.md). Why am I getting this error? Any help will be appreciated. Thanks.
import 'reflect-metadata';
import { Container, multiInject, injectable, } from 'inversify';
interface IWeapon {
name: string;
}
interface INinja {
weapons: IWeapon[],
displayWeapons(): void,
}
@injectable()
class Katana implements IWeapon {
public name = 'Katana';
}
@injectable()
class Shuriken implements IWeapon {
public name = 'Shuriken';
}
@injectable()
class Ninja implements INinja {
public weapons: IWeapon[];
constructor(
@multiInject('Weapon') _weapons: IWeapon[],
) {
this.weapons = _weapons;
}
public displayWeapons = () => {
console.log(this.weapons[0].name, this.weapons[1].name);
}
}
const container = new Container();
container.bind<INinja>("Ninja").to(Ninja);
container.bind<IWeapon>("Weapon").to(Katana);
container.bind<IWeapon>("Weapon").to(Shuriken);
const ninja = container.get<INinja>('Weapon');
ninja.displayWeapons(); // Should display all weapons.
Share
Improve this question
asked Apr 19, 2020 at 1:39
Syed Hammad AhmedSyed Hammad Ahmed
2593 silver badges9 bronze badges
1 Answer
Reset to default 4You are getting "Ambiguous match found for serviceIdentifier" because when you get the container you are using the "weapon" identifier and that is not correct.
Changing this line const ninja = container.get<INinja>('Weapon');
to const ninja = container.get<INinja>('Ninja');
should give you the desired output.
本文标签:
版权声明:本文标题:javascript - InversifyJS @multiInject not working, throws error "Ambiguous match found for serviceIdentifier" 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744611539a2615686.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论