Javascript 如何仅使用Nest.js framework中的依赖项注入容器?
是否可以只使用Nest.js框架中的DI和IoC功能?如果是,如何做到这一点 我试图以这种方式实施它:Javascript 如何仅使用Nest.js framework中的依赖项注入容器?,javascript,dependency-injection,nestjs,Javascript,Dependency Injection,Nestjs,是否可以只使用Nest.js框架中的DI和IoC功能?如果是,如何做到这一点 我试图以这种方式实施它: import { NestFactory } from "@nestjs/core"; import { Module, Injectable } from "@nestjs/common"; @Injectable() class AppRepository { sayHi() { console.log("app repository"); console.log("
import { NestFactory } from "@nestjs/core";
import { Module, Injectable } from "@nestjs/common";
@Injectable()
class AppRepository {
sayHi() {
console.log("app repository");
console.log("Hello");
}
}
@Injectable()
class AppService {
constructor(private appRepository: AppRepository) {}
sayHi() {
console.log("app service");
this.appRepository.sayHi();
}
}
@Module({
imports: [],
providers: [AppService, AppRepository]
})
class AppModule {
constructor(private appService: AppService) {}
sayHi() {
console.log("app module");
this.appService.sayHi();
}
}
async function bootstrap() {
const app = await NestFactory.createApplicationContext(AppModule);
const module = app.get<AppModule>(AppModule);
module.sayHi();
}
bootstrap();
从“@nestjs/core”导入{NestFactory};
从“@nestjs/common”导入{Module,Injectable}”;
@可注射()
阶级认同{
(){
console.log(“应用程序存储库”);
console.log(“你好”);
}
}
@可注射()
类应用程序服务{
构造函数(私有appropository:appropository){}
(){
console.log(“应用程序服务”);
这个.appRepository.sayHi();
}
}
@模块({
进口:[],
提供者:[应用程序服务,应用程序]
})
类AppModule{
构造函数(私有appService:appService){}
(){
console.log(“应用程序模块”);
this.appService.sayHi();
}
}
异步函数引导(){
const app=wait NestFactory.createApplicationContext(AppModule);
const module=app.get(AppModule);
module.sayHi();
}
bootstrap();
但当我运行代码时,我得到:
app module
(node:70976) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'sayHi' of undefined
at AppModule.sayHi (/Users/jakub/projects/nest-di-clean/build/main.js:47:25)
at /Users/jakub/projects/nest-di-clean/build/main.js:60:16
at Generator.next (<anonymous>)
at fulfilled (/Users/jakub/projects/nest-di-clean/build/main.js:11:58)
at process._tickCallback (internal/process/next_tick.js:68:7)
at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
app模块
(节点:70976)未处理的PromisejectionWarning:TypeError:无法读取未定义的属性“sayHi”
在AppModule.sayHi(/Users/jakub/projects/nest di clean/build/main.js:47:25)
at/Users/jakub/projects/nest di clean/build/main.js:60:16
在Generator.next()处
完成时(/Users/jakub/projects/nest di clean/build/main.js:11:58)
在进程中。_tick回调(内部/process/next_tick.js:68:7)
位于Function.Module.runMain(internal/modules/cjs/loader.js:834:11)
启动时(内部/bootstrap/node.js:283:19)
在bootstrapNodeJSCore(internal/bootstrap/node.js:623:3)
所以我得到了AppModule
的实例,但是没有注入AppService
的实例
我想在一个不需要控制器和其他服务器端东西的库中使用它。我找到了一个解决方案。我没有用
nest cli
创建新项目,只是用TypeScript创建了一个空的npm项目,并添加了@nestjs/common
和@nestjs/core
作为依赖项(我希望最小化依赖项)。我的错误是在tsconfig.json
文件中,我忘了允许emitdecormatadata
。以下是我的package.json
和tsconfig.json
文件,供感兴趣的人参考:
package.json
{
"name": "nest-di-clean",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"compile": "yarn tsc",
"start": "node ./build/main.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"@nestjs/common": "^6.11.5",
"@nestjs/core": "^6.11.5",
"reflect-metadata": "^0.1.13",
"rxjs": "^6.5.4"
},
"devDependencies": {
"@types/node": "^13.7.0",
"typescript": "^3.7.5"
}
}
{
"compilerOptions": {
"target": "ES2015",
"module": "commonjs",
"outDir": "./build",
"strict": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"forceConsistentCasingInFileNames": true
}
}
tsconfig.json
{
"name": "nest-di-clean",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"compile": "yarn tsc",
"start": "node ./build/main.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"@nestjs/common": "^6.11.5",
"@nestjs/core": "^6.11.5",
"reflect-metadata": "^0.1.13",
"rxjs": "^6.5.4"
},
"devDependencies": {
"@types/node": "^13.7.0",
"typescript": "^3.7.5"
}
}
{
"compilerOptions": {
"target": "ES2015",
"module": "commonjs",
"outDir": "./build",
"strict": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"forceConsistentCasingInFileNames": true
}
}
main.js
文件的内容都在我的问题中。您使用的是什么版本的Nest?旋转一个新项目,复制粘贴你的代码,我没有errors@JayMcDoniel谢谢你尝试一下。我的回答中描述了一个错误的tsconfig。