typescript - Create component from a string -
i have components i'd add page dynamically.
i have name in config file config.json
, typescript classname mysupercomponent
. can put want in config file class name not suited.
i've tried code component resolver, can't pass string it.
this works:
this.resolver.resolvecomponent(mysupercomponent)
this doesn't:
this.resolver.resolvecomponent("mysupercomponent")
how can instanciate (and inject) component string ?
you can set map , provide service translates string type
import {mysupercomponent} './mysupercomponent'; @injectable() class componenttypes { types = { mysupercomponent: mysupercomponent }; totype(name:string) { return types[name]; } }
where use it
@component({ ... providers: [componenttypes], }) class somecomponent { constructor(private componenttypes:componenttypes) {} adddynamiccomponent() { ... this.resolver.resolvecomponent(this.componenttypes.totype("mysupercomponent")) } }
Comments
Post a Comment