【发布时间】:2019-09-05 04:12:45
【问题描述】:
我遇到了奇怪的问题, Container.get(MyServiceName);返回请求的服务,但用@Inject() 修饰的类属性未定义。
我试过用@Service() 装饰类
我确实在我的应用程序的入口点导入反射元数据 导入“反射元数据”;
我想知道这是否与我使用 typedi 不是直接来自我的 node_modules 而是来自我的依赖项的事实有关?
应用程序既不使用 Express 框架也不使用路由控制器
我的 tsconfig.json:
"compilerOptions": {
"declaration": true,
"pretty": true,
"esModuleInterop": true,
"target": "esnext",
"noImplicitAny": true,
"noImplicitReturns": true,
"noUnusedLocals": false,
"moduleResolution": "node",
"noUnusedParameters": true,
"strictPropertyInitialization": false,
"module": "commonjs",
"lib": ["dom", "es2018"],
"importHelpers": true,
"outDir": "./dist",
"strict": true,
"typeRoots": ["node_modules/@types"],
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"sourceMap": true
},
"include": ["./src/**/*"],
"exclude": ["node_modules", "./dist/**/*"]
}
我要注入的服务
import MyServiceName from '../services/MyServiceName';
import { Container, Inject, Service } from 'my_dependency/lib';
export default class SomeClass{
@Inject()
private readonly someService: MyServiceName;
public async someMethod(): SomeResponseType {
const thatWorks = Container.get<MyServiceName>(MyServiceName);
console.log(this.someService); // undefined
console.log(thatWorks); // proper class
}
}
我要注入的服务
@Service()
export default class MyServiceName {
public async someMethod(): SomeReturnType {
return someReturnedData;
}
}
我想通过 @Inject() 装饰器注入我的依赖项
【问题讨论】:
标签: node.js typescript