【发布时间】:2021-11-28 10:23:12
【问题描述】:
我的项目是使用 Express 的后端。我试图使用装饰器创建一个自动招摇。然后我创建了一个名为decorators.ts 的文件和一个名为dynamic-loader.ts 的文件。
我的动态加载器只是在控制器文件夹内的每个文件中使用import。在我的decorators.ts 中,我有许多装饰器,如下所示:
...
export function ApiController(route: string, name?: string) {
return function (
target: any
) {
target.prototype.route = route;
target.prototype.tag = name ?? (<string>target.name).replace(/Controller/g, '');
};
}
export function AllowAnonymous(
target: any,
propertyKey: string,
descriptor: PropertyDescriptor
) {
descriptor.value.anonymous = true;
}
...
问题是,每当我将任何装饰器导入控制器时,都会出现此错误:
Error: Cannot find module 'src/lib/decorators'
我的 tsconfig.json 看起来像这样:
{
"compilerOptions": {
"baseUrl": "./",
"lib": [
"es5",
"es6"
],
"target": "es2020",
"module": "commonjs",
"outDir": "dist",
"moduleResolution": "node",
"resolveJsonModule": true,
"strict": true,
"strictNullChecks": false,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true,
"allowJs": true
},
"include": [ "src/**/*" ],
"exclude": [ "node_modules" ]
}
【问题讨论】:
标签: node.js typescript typescript-decorator