我来自非打字稿和非巢背景。我正在检查代码,发现了这段代码片段
import { Inject, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { AreaService } from '../area/-area.service';
import { Repository } from 'typeorm';
import { Office } from './office.entity';
import { OfficeInterface } from './office.interface';
@Injectable()
export class OfficeService {
constructor(
@Inject(AreaService)
private readonly AreaService: AreaService,
@InjectRepository(Office)
private readonly OfficeRepository: Repository<Office>,
) {}
老实说,现在这对我来说是压倒性的。我去了嵌套 JS 页面 https://docs.nestjs.com/fundamentals/injection-scopes了解他们要说的这个和这个
对于来自不同编程语言背景的人来说
可能会出乎意料地发现,在 Nest 中,几乎所有内容都是共享的
跨传入请求。我们有一个到数据库的连接池,
具有全局状态的单例服务等。请记住 Node.js
不遵循请求/响应多线程无状态模型
每个请求都由单独的线程处理。因此,使用
单例实例对于我们的应用程序来说是完全安全的
他们在上面的声明中是否意味着他们将所有内容添加到请求对象中nestJS
?
import { Injectable, Scope } from '@nestjs/common';
@Injectable({ scope: Scope.REQUEST })
export class CatsService {}
Specify injection scope by passing the scope property to the @Injectable() decorator options object:
所以如果我们这样做@Injectable()
它的范围有多大?
有人可以解释一下两者之间的区别吗@Injectable()
, @Inject(AreaService)
and @InjectRepository(Office)
以及何时、应该使用哪一个?