Angular 2 - Resolve Component Factory with a string

前端 未结 1 1674
南笙
南笙 2020-12-19 23:11

This is for the 2.1.0 Final release versions of Angular.

I\'m trying to dynamically instantiate a component who\'s type is passed from a config JSON file. The best t

相关标签:
1条回答
  • 2020-12-19 23:33

    My solution for now is to do what I really wanted to avoid, which is maintaining a key/val registry.

    let componentRegistry = {
        'SomeComp1': SomeComp1, 
        'SomeComp2': SomeComp2
    }
    

    And then call it with:

    private renderComponent(config:any) {
        let configComponents = config.components;
        if(config.hasOwnProperty('components') && configComponents.length){
            for(let i = 0, len = configComponents.length; i < len; i++){
                let comp = configComponents[i];
                this.componentResolver.resolveComponentFactory(this.componentRegistry[comp]);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题