我正在尝试将JavaScript迁移到TypeScript(AngularJS到Angular4)。
JavaScript代码片段:
var className = eval('MethodName');
var data = new className();
In,我已经迁移的Typescript代码如下,
let code = ts.transpile('MethodName');
var className = eval(code);
var data = new className();
我面临以下错误:
ReferenceError: MethodName is not defined
at eval (eval at <anonymous> (main.ts:457), <anonymous>:1:1)
at main.ts:457
at Array.forEach (<anonymous>)
at Editor.webpackJsonp.603.Editor.loadPlugins (main.ts:452)
at Editor.<anonymous> (main.ts:123)
at ZoneDelegate.invokeTask (zone.js:421)
at Object.onInvokeTask (core.es5.js:3881)
at ZoneDelegate.invokeTask (zone.js:420)
at Zone.runTask (zone.js:188)
at ZoneTask.invokeTask (zone.js:495)
我已经在TypeScript文件中导入了这个类。
import * as ts from 'Typescript';
import { MethodName } from './method.ts';