and*_*eas 6 javascript oop class typescript angular
在我的应用程序中,我有这样的事情:
用户.ts
export class User { ... }
Run Code Online (Sandbox Code Playgroud)
现在,我这样做:
app.component.ts
callAnotherFunction(User);
Run Code Online (Sandbox Code Playgroud)
如果我将类名作为字符串,我该怎么做"User"?如果可能,我将如何检查变量是否真的是一个可用的类?
例如:
let test = "User";
if (**test is really a usable class**) {
console.log("Yay!");
callAnotherFunction(**something to put here**);
} else {
console.log("Error!");
}
Run Code Online (Sandbox Code Playgroud)
我不确定这是否可能,但已经感谢您的投入。
到目前为止,我发现的只是这样的例子:
let test = "User";
let user = new window[test]();
Run Code Online (Sandbox Code Playgroud)
但user随后将是User. 此外,它似乎在我的 Angular 2 应用程序中不起作用 - 可能是范围错误。如果我想尝试这个,我需要用什么来代替window?