用配置文件导入Typescript模块



我想用一些配置文件伪造访问node_modules的路径。似乎不可能做到下面的事情,我该如何开拓自己的道路呢?

目的是写一些node_modules的绝对路径(因为有些文件被分割了,所以需要这个)。

import someFileSettings from "./../../models/someFileSettings";
import * as request from JSON.stringify(someFileSettings .somePathIneed+"request");

不,这是不可能的,因为TypeScript模块遵循标准的ES6模块。要做到这一点,有一个模块加载器API,基于承诺。

以下是Axel Rauschmayer博士的书中的解释:

16.9.1我可以使用变量来指定从哪个模块导入吗?

import语句是完全静态的:它的模块说明符总是固定的。如果你想动态地决定加载什么模块,你需要使用编程加载器API:

const moduleSpecifier = 'module_' + Math.random();
System.import(moduleSpecifier)
.then(the_module => {
    // Use the_module
})

…但是,请注意以下警告:

模块加载器API不是ES6标准的一部分

最新更新