2017-02-18 92 views
1

目前我的解決方案是通過導入來動態生成的,還有其他方法不能通過導入?In Typescript如何做C#Activator.CreateInstance()

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](); 
 
    }); 
 
    } 
 
    
 
}

回答

1

TypeScript文件是一個模塊,無需引用模塊就無法訪問其導出。您可以使用index.ts並重新導出所有類別以最大限度地減少進口量。

你也可以玩WebPacks System.import,看到這個answer

+1

[現場演示](https://plnkr.co/edit/Mf3uEQjjtuq4VUPXpY1h?p=preview)你的意思是? –