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"))    } } 

see angular2, manually resolve type string/name


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -