我想用一些配置文件伪造访问node_modules的路径。看来下面的事情是不可能做到的,那我该如何打造我的道路呢?
目的是写入一些node_modules的绝对路径(因为一些文件被分割,所以这是需要的)。
import someFileSettings from "./../../models/someFileSettings";
import * as request from JSON.stringify(someFileSettings .somePathIneed+"request");
最佳答案
不,这是不可能的,因为 TypeScript 模块遵循标准 ES6 模块。为此,有一个基于 promise 的模块加载器 API。
这是 the book of Dr. Axel Rauschmayer 的解释:
16.9.1 Can I use a variable to specify from which module I want to import?
The import statement is completely static: its module specifier is always fixed. If you want to dynamically determine what module to load, you need to use the programmatic loader API:
const moduleSpecifier = 'module_' + Math.random(); System.import(moduleSpecifier) .then(the_module => { // Use the_module })
...但是,请注意此警告:
The module loader API is not part of the ES6 standard
关于node.js - 使用配置文件导入 Typescript 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39506429/