0

At present my solution is through the import to dynamically generated, there are other ways to not through the import?

live demo


abc.ts

export class abc{
  constructor(){
    alert('hello world');
  }
}

export class abc2{
  constructor(){
    alert('hello world2');
  }
}

app.component.ts

...
import * as myModule from './abc';

@Component({
  ...
})
export class AppComponent { 
  
  createAllClass(){
    let allClassName = ['abc','abc2'];
    allClassName.forEach(className => {
      new myModule[className]();
    });
  }
  
}
Chunbin Li
  • 2,196
  • 1
  • 17
  • 31

1 Answers1

1

TypeScript file is a module, without referencing module it is impossible to access its exports. You could use index.ts and re-export all classes to minimize amount of imports.

You could also play with WebPacks System.import, see this answer.

Community
  • 1
  • 1
kemsky
  • 14,727
  • 3
  • 32
  • 51