【发布时间】:2019-11-10 09:36:03
【问题描述】:
我在名为“controllers”的文件夹中有一些类。从我的“main.ts”, 我列出了该“控制器”文件夹中的类并尝试动态导入它们。
每个类都使用“export default”导出
我尝试过的:
- 从要导入的类中删除导出默认值
结果:
- 不抛出任何错误
- Foo 值:
{ default: {} }
./controllers/LoginController.ts
export default class LoginController {
...
...
...
}
./main.ts
glob("**/*.controller.ts", {}, async function (er, paths: string[]) {
// files is an array of filenames.
// If the `nonull` option is set, and nothing
// was found, then files is ["**/*.js"]
// er is an error object or null.
for (const path of paths) {
try {
const foo = require(`../${path}`)
console.log(foo)
} catch (e) {
console.log(e)
}
}
});
这是我运行东西时的终端输出:
事情是打字稿我使用它来动态导入任何类我得到以下错误:
export default class LoginController {
[0] ^^^^^^
[0]
[0] SyntaxError: Unexpected token export
[0] at Module._compile (internal/modules/cjs/loader.js:703:23)
[0] at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10)
[0] at Module.load (internal/modules/cjs/loader.js:628:32)
[0] at Function.Module._load (internal/modules/cjs/loader.js:555:12)
[0] at Module.require (internal/modules/cjs/loader.js:666:19)
[0] at require (internal/modules/cjs/helpers.js:16:16)
[0] at /Users/absystech/Development/Absystech/espace client/backend/dist/main.js:25:29
[0] at Generator.next (<anonymous>)
[0] at /Users/absystech/Development/Absystech/espace client/backend/dist/main.js:8:71
[0] at new Promise (<anonymous>)
有人可以帮我解决这个问题吗,提前谢谢!?
【问题讨论】:
标签: typescript class dynamic import