简体   繁体   中英

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.

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. Is it possible to get an instance of OrderService without converting the product module to a NestJS module?

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.

It's just an example for include 'apn' module (that is not nestjs native)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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