非模块文件中的NestJS Inject模块服务 [英] NestJS Inject module service inside in a non module file

查看:65
本文介绍了非模块文件中的NestJS Inject模块服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于视图渲染的功能文件,我想在这里使用nestjs模块服务.我的渲染文件就是这样.

I have a function file for view render and i want to use nestjs modules service in here. My render file is like this.

export default {
  parse() { }
  render() { }
}

所以要在这里使用模块服务,我试图像这样注入.

So to use module service in here i tried to inject like this.

import { Inject } from '@nestjs/common';
import { MemberService } from './member.service';

class SampleClass {
  constructor(@Inject('MemberService') private readonly memberService: MemberService) {}
}

// Creating class in here to use in functions
const sampleService = new SampleClass();

export default {
  parse() { }
  render() { }
}

当我尝试在此处创建新类时,出现错误预期有1个参数但有0个"

When i try to create new class in here i got error "Expected 1 arguments but got 0"

我的MemberService就是这样

My MemberService is like this

@Injectable()
export class MemberService {
  constructor(@Inject(Constants.RelationshipMemberModel) private readonly relationshipMemberModel: typeof Model) {}
  login () {}
}

那么我应该如何在该文件中注入服务以使用?

So how should i inject service to use in this file ?

推荐答案

为了注入提供程序,组件必须属于nest应用程序中的 context (基本上是一个模块).必须是可注射控制器异步提供程序自定义异步提供程序,并且它必须属于可以访问您要注入的提供程序的模块.为了在不在上下文中的外部javascript类中使用服务的功能,您可以导入服务,并在此处自行实例化它,您可以使用此方法的示例:

In order to Inject a provider the component has to belong to a context (Basically a module) in the nest application, basically it has to be or a Injectable, Controller, Async Provider or a Custom Async Provider, and it has to belong to a Module in which it has access to the provider you are trying to inject. In order to use a functionality of a service in external javascript classes that are not in a context you can import the service and you gotta instantiate it by yourself here you can have an example of this aproach:

class SampleClass {
  private memberService: MemberService; 
  constructor() {
    this.memberService = new MemberService();
  }
}

请记住,这是该服务的另一个实例,如果您不小心,则会在运行时拥有多个实例,因为它不再是可注入的,而是一个类对象.为了防止这种情况,您可以创建一个包含所有MemberService功能的 singleton 并将其导入MemberService和SampleClass中:

Remember this is another instance of the service and if you don't take care you will have multiple instances on runtime as it is not an injectable anymore but a class object. In order to prevent this maybe you can create a singleton containing all the MemberService functionality and import it in both the MemberService and the SampleClass:

export class MemberFunctionality {
  private static memberFunctionality: MemberFunctionality;
  private constructor() {}
  static getInstance(): MemberFunctionality {
    if(!memberFunctionality) {
      this.memberFunctionality = new MemberFunctionality();
    }
    return this.memberFunctionality;
  }
  login() {}
}

然后将其导入MemberService和SampleClass上,并调用getInstance方法

Then you import it on both MemberService and SampleClass and call the getInstance method

import { MemberFunctionality } from './member-functionality.ts';

class SampleClass {
  private memberFunctionality: MemberFunctionality;
  constructor() {
    this.memberFunctionality = MemberFunctionality.getInstance();
  }
  ...
}

MemberService也一样

same goes for the MemberService

import { MemberFunctionality } from './member-functionality.ts';

@Injectable()
export class MemberService {
  private memberFunctionality: MemberFunctionality;
  constructor(@Inject(Constants.RelationshipMemberModel) private readonly relationshipMemberModel: typeof Model) {
    this.memberFunctionality = MemberFunctionality.getInstance();
  }
  login () {
    return this.memberFunctionality.login();
  }
}

这将只包含该功能的一个实例

And that would take care of only having an instance of that functionality

这篇关于非模块文件中的NestJS Inject模块服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆