I am trying to have templates automagically wired to an inversifyjs container but whatever I try it's not working. Please help?
private templates = [
{file: './component.html.tpl', obj: 'HtmlTemplate'},
{file: './component.tpl.ts', obj: 'ComponentTemplate'}
];
private container = new Container();
bind(){
// original and working code
// this.container.bind<ITemplate>('HtmlTemplate').to(HtmlTemplate);
// this.container.bind<ITemplate>('ComponentTemplate').to(ComponentTemplate);
this.templates.forEach(template => {
import(template.file).then(mod => {
console.log(mod.default, template);
// is this correct (seems to work) =>
this.container.bind<ITemplate>(template.obj).to(mod.default);
console.log('bound =>', mod.default);
});
});
}
and files ./component.html.tpl
@injectable() export default class HtmlTemplate implements ITemplate { ... }
and ./component.ts.tpl
@injectable() export default class ComponentTemplate implements ITemplate { ... }
Which logs completely as expected to the console:
[Function: HtmlTemplate] { file: './component.html.tpl', obj: 'HtmlTemplate' }
[Function: ComponentTemplate] { file: './component.tpl.ts', obj: 'ComponentTemplate' }
But I really expected the code in the foreach statement:
this.container.bind<ITemplate>(template.obj).to(mod.default);
to be equivalent to this:
this.container.bind<HtmlTemplate>('HtmlTemplate').to(HtmlTemplate);
this.container.bind<ComponentTemplate>('ComponentTemplate').to(ComponentTemplate);
but when I try to resolve it in an other loop:
this.templates.forEach(template => {
const tpl = this.container.get<ITemplate>(template.obj);
...
it throws an error:
Error: No matching bindings found for serviceIdentifier HtmlTemplate
Anyone know how to solve this?