如何将对象传递给角度为2/4 app的NgModule中的服务构造函数

时间:2022-04-27 12:47:27

So here is a service say MyService

所以这是一个服务说MyService

MyService has a constructor in its implementation

MyService在其实现中有一个构造函数

@Injectable() 
export class MyService {
    constructor(myObject: MyClass) {}
}

Now myObject is of type MyClass which I need to pass while injecting the service

现在myObject是MyClass类型,我需要在注入服务时传递它

One way by which I can use MyService is

我可以使用MyService的一种方法是

_myService = new MyService(new Myclass())

and then access methods in MyService

然后访问MyService中的方法

this._myService.someMethod();

But with this approach I have to do this in every component where I use MyService

但是使用这种方法,我必须在我使用MyService的每个组件中执行此操作

I want to pass the value to MyService constructor in NgModule in providers array

我想将值传递给providers数组中NgModule中的MyService构造函数

So that in any component I need MyService I can just use by

所以在我需要MyService的任何组件中我都可以使用

export class MyComponent {
    constructor( _myService: MyService) {}
    this._myService.someMethod();
}

2 个解决方案

#1


0  

One way is to make the Myclass object also an injectable using ValueProvider

一种方法是使用ValueProvider使Myclass对象也成为可注入的

{ provide: Myclass, useValue: new Myclass() }

and add it to the providers. Then DI mechanism can know about the Myclass type and inject correctly. The constructor of service must provide the type for the parameter

并将其添加到提供程序。然后DI机制可以知道Myclass类型并正确注入。服务的构造函数必须提供参数的类型

export class MyService {    
   constructor(myObject: MyClass) {}    
}

and inject MyService as you want

并根据需要注入MyService

export class MyComponent {
   constructor( _myService: MyService) {}
}

Also you can just create that instance in the service if it is the same for all of them and omit from the constructor.

您也可以在服务中创建该实例,如果它们对所有实例都相同,则从构造函数中省略。

#2


0  

So I figured out how to do it

所以我想出了如何做到这一点

It can be done by using useFactory in the following manner

可以通过以下方式使用useFactory来完成

{
  provide: MyService, 
  useFactory: MyServiceFactory  
}

export function MyServiceFactory() {
  return new MyService(new MyClass());
}

This will pass the MyClass object which is required by MyService constructor and the service can be injected henceforth into any component.

这将传递MyService构造函数所需的MyClass对象,并且此后可以将服务注入任何组件。

export class MyComponent {
  constructor( _myService: MyService) {}
}

#1


0  

One way is to make the Myclass object also an injectable using ValueProvider

一种方法是使用ValueProvider使Myclass对象也成为可注入的

{ provide: Myclass, useValue: new Myclass() }

and add it to the providers. Then DI mechanism can know about the Myclass type and inject correctly. The constructor of service must provide the type for the parameter

并将其添加到提供程序。然后DI机制可以知道Myclass类型并正确注入。服务的构造函数必须提供参数的类型

export class MyService {    
   constructor(myObject: MyClass) {}    
}

and inject MyService as you want

并根据需要注入MyService

export class MyComponent {
   constructor( _myService: MyService) {}
}

Also you can just create that instance in the service if it is the same for all of them and omit from the constructor.

您也可以在服务中创建该实例,如果它们对所有实例都相同,则从构造函数中省略。

#2


0  

So I figured out how to do it

所以我想出了如何做到这一点

It can be done by using useFactory in the following manner

可以通过以下方式使用useFactory来完成

{
  provide: MyService, 
  useFactory: MyServiceFactory  
}

export function MyServiceFactory() {
  return new MyService(new MyClass());
}

This will pass the MyClass object which is required by MyService constructor and the service can be injected henceforth into any component.

这将传递MyService构造函数所需的MyClass对象,并且此后可以将服务注入任何组件。

export class MyComponent {
  constructor( _myService: MyService) {}
}