【发布时间】:2020-11-22 02:59:48
【问题描述】:
我来自非打字稿和非嵌套背景。我在查看代码时发现了这段代码sn-p
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>,
) {}
现在,老实说,这对我来说是压倒性的。我去了Nest JS page 了解这一点以及他们要说的话
对于来自不同编程语言背景的人来说,它 得知在 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) 和 @InjectRepository(Office) 之间的区别以及何时应该使用哪一个?
【问题讨论】:
标签: typescript nestjs