如何通过提供类名字符串文字来获取类?

时间:2017-05-24 01:26:16

标签: javascript

如何替换XXX,以便为要导入的对应类评估替换?如果name是“FooService”,则将评估导入的FooService类并在XXX中使用。

Class FooService:

export class FooService {}

Class BarService:

export class BarService {}

类容器:

import {FooService} from './FooService'
import {BarService} from './BarService'

export class Container {
    constructor() {
        this._services = new Map();
    }
    getServiceByName(name) {
        if (!this._services.has(name)) {
         // How to replace XXX so that the replacement will 
         // be evaluated for the correspondent class that get imported?
         // e.g. if name is "FooService", then the imported FooService 
         // class will be evaluated and used in XXX.
           this._services.set(name, XXX); 
        }

        return this._service.get(name);
    }
}

0 个答案:

没有答案