从字符串创建一个类

时间:2019-02-27 07:07:02

标签: angular typescript

我有这个界面:

export interface Base {
  name: string;
}

还有一个班级

export class SampleClass implements Base {
  name = 'Testing';
  constructor() {}
}

然后在另一个类中,我尝试从字符串创建类'SampleClass':

const sampleClass = Object.create(window['SampleClass'].prototype);

但是出现错误:

ERROR TypeError: Cannot read property 'prototype' of undefined

然后我尝试了另一种方式:

const secClass: any = new (<any>window)['SampleClass']();

并收到此错误:

ERROR TypeError: window.SampleClass is not a constructor

如何从字符串正确创建对象?

0 个答案:

没有答案
相关问题