[英]NestJS Injecting request or execution context in services
如何在服务中注入请求或执行上下文?
Since version 6.0, current request can be injected into a service, with REQUEST
token :从 6.0 版本开始,当前请求可以使用
REQUEST
令牌注入到服务中:
export class AppService {
constructor(@Inject(REQUEST) private request) {}
load() {
const user = this.request.user;
}
}
You can inject the request with the @Req()
decorator in a controller and then pass the request to a service. 您可以在控制器中使用
@Req()
装饰器注入请求,然后将请求传递给服务。
@Get()
findAll(@Req() request) {
return this.service.doStuff(request);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.