Nest js 中的作用域如何工作

问题描述 投票:0回答:1

根据文档,如果服务是请求范围,则控制器依赖于它,并且将自动成为请求范围。

Imagine the following dependency graph: CatsController <- CatsService <- CatsRepository. If CatsService is request-scoped (and the others are default singletons), the CatsController will become request-scoped as it is dependent on the injected service. The CatsRepository, which is not dependent, would remain singleton-scoped.

import { Injectable, Scope, Inject } from '@nestjs/common';
import { REQUEST } from '@nestjs/core';
import { Request } from 'express';

export class CatsService {
  constructor(@Inject(REQUEST) private request: Request) {}
}

所以在示例中,我共享 CatsService 注入的 REQUEST 这是请求范围,那么 CatsService 服务应该自动请求范围?

javascript node.js nestjs nest nestjs-config
1个回答
0
投票

您的理解是正确的。

REQUEST
是请求对象的自定义提供者的注入令牌,并设置为
scope: Scope.REQUEST
,这意味着它注入的任何内容都将成为
REQUEST
作用域

© www.soinside.com 2019 - 2024. All rights reserved.