简体   繁体   English

如何在 Nest.js 之外手动获取提供者的实例

[英]How to manually get an instance of provider outside of Nest.js

I have an Express.js application that I'm migrating to Nest.js, some application modules are using Nest.js, some aren't.我有一个要迁移到 Nest.js 的 Express.js 应用程序,一些应用程序模块正在使用 Nest.js,有些则没有。

Let's say I have an OrderModule which is a Nest.js module and it has an OrderService in its providers and I would like to use OrderService in a product module that is not a NestJS module.假设我有一个 OrderModule,它是一个 Nest.js 模块,它的提供程序中有一个 OrderService,我想在不是 NestJS 模块的产品模块中使用 OrderService。 Is it possible to get an instance of OrderService without converting the product module to a NestJS module?是否可以在不将产品模块转换为 NestJS 模块的情况下获取 OrderService 的实例?

The only way to get access to Nest's DI container from outside the application would be to export the app instance created by NestFactory.create() and then call app.get(OrderService) to retrieve the service.从应用程序外部访问 Nest 的 DI 容器的唯一方法是导出由NestFactory.create()创建的app实例,然后调用app.get(OrderService)来检索服务。

It's just an example for include 'apn' module (that is not nestjs native)这只是包含'apn'模块的一个例子(不是nestjs原生的)

import { Injectable } from '@nestjs/common';
import { UserGateway } from '../../users/gateways/user.gateway';

@Injectable()
export class NotificationsGateway {
    private user: UserGateway;
    private appleapn = require('apn');
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM