typescript - 如何在 TypeScript 的循环内导入外部模块?

标签 typescript

我正在尝试在循环内导入“ui”(用于基于 _moduleList 的动态加载。这很好用:

var _moduleList: Array<string>;
_moduleList.push("mdlGame");
import _tmp = require("ui");
for (var _i: number = 0; _i < _moduleList.length; ++_i) {

}

而这会在 import 下方显示一条红色波浪线,表示“意外的 token ;需要‘语句’。”:

var _moduleList: Array<string>;
_moduleList.push("mdlGame");
for (var _i: number = 0; _i < _moduleList.length; ++_i) {
    import _tmp = require("ui");
}

import 不算语句吗?这是怎么回事,我有办法解决它吗?

最佳答案

时间过去了,但 OP 的问题 功能 仍然存在。

但是,我只是找到了部分解决方法(使用 namespace import pattern ),如本例所示。

我正在导入一个 index.ts 文件,写成这样:

    import { A } from './some/a';
    import { B } from './some/other/b';
    export { A, B };                     // you'll save this list
    export const LIST: any[] = [ A, B ]; // and this other too

保存这两个列表是我的目的,因为它们每个都有数十个项目并且还在不断增长。

  • 模块.ts

    export { A } from './some/a';
    export { B } from './some/other/b';
    
  • list.ts

    import * as modules from './modules';
    export const LIST: any[] = Object.keys(modules).map(key => modules[key]);
    
  • index.ts

    export * from './modules';
    export * from './list';
    

一切都按预期工作,而且完全干了。

关于typescript - 如何在 TypeScript 的循环内导入外部模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19285988/

相关文章:

javascript - ionic 日期时间 : How to get date value without timestamp?

angular - 在 Angular 中单击 'None of the above option' 时,有没有办法重置复选框项目的其余部分?

javascript - 使用 Typescript 时,如何从子 Controller 访问父 Controller 中的作用域函数?

typescript - 如何为 lodash/fp 管道传输 Promise.allSettled? (或任何类似管道库的功能,例如 : p-pipe)

关于 Express 请求的 Typescript 声明合并失败

reactjs - 在 React + Redux 客户端应用程序上保存事件用户数据的位置

angular - Ag-grid:如何在使用 AggFunc 时更改定义列 headerName?

express:扩展 Express.Request 的项目本地中间件

javascript - Sequelize V5->V6 升级 : Typescript Error Property 'x' does not exist on type 'y'

javascript - Typescript 声明文件不是模块