简体   繁体   English

如何从动作中注入Angular 2服务中的提供程序?

[英]How to inject a provider in Angular 2 service from action?

I am trying to remove the dependency of components on services in Angular with the help of Redux. 我试图在Redux的帮助下删除Angular中服务上组件的依赖性。

Basically, the flow is Component -> Action -> Service 基本上,流程是Component - > Action - > Service

In the service I want to use http module of @angular/core, which is suggested to be passed in the constructor: 在服务中我想使用@ angular / core的http模块,建议在构造函数中传递:

export class SampleService {
    constructor(public http: Http) {}
}

And as I am calling service from action, it won't get the http provider as I do not have an instance of http provider in it. 因为我从行动中调用服务,所以它不会获得http提供者,因为我没有http提供者的实例。

export default class SampleAction {
    private _projectService;
    constructor() {
        this._sampleService = new SampleService();
    }
}

How can I inject the http provider into the service? 如何将http提供程序注入服务?

In your action you can inject the Http in constructor and pass it into the service instance. 在您的操作中,您可以在构造函数中注入Http并将其传递到服务实例中。 Something like 就像是

export default class SampleAction {
    private _projectService;
    constructor(@Inject(Http) http: Http) {
        this._sampleService = new SampleService(http);
    }    
}

In fact you don't need to instantiate the service by your own. 实际上,您不需要自己实例化服务。 Just inject the service into a component or another service. 只需将服务注入组件或其他服务即可。 What is important is to have the provider configured for this execution flow. 重要的是为此执行流程配置提供程序。 In your case, Http (and more generally HTTP_PROVIDERS ) and SampleSampleService . 在您的情况下,Http(更SampleSampleServiceHTTP_PROVIDERS )和SampleSampleService Providers are defined for the whole application (at the bootstrap level) or for component ( providers attribute). 为整个应用程序(在引导级别)或组件( providers属性)定义providers

bootstrap(AppComponent, [ HTTP_PROVIDERS, SampleService ]);

or 要么

@Component({
  providers: [ HTTP_PROVIDERS, SampleService ]
})
export class SomeComponent {
  constructor(private action: SampleAction) {
  }

  executeAction() {
    this.action.doSomething();
  }
}

Here is the code you should use for your SampleAction class: 以下是您应该为SampleAction类使用的代码:

export default class SampleAction {
  constructor(private _projectService: SampleService) {
  }
}

You can notice that to be able to inject into a service, the @Injectable need to be used. 您可以注意到,为了能够注入服务,需要使用@Injectable

@Injectable()
export class SampleService {
  constructor(public http: Http) {}
}

This answer could give you more hints about the way to use dependency injection and how hierarchical injectors work in Angular2: 这个答案可以为您提供更多关于使用依赖注入的方法以及分层注入器如何在Angular2中工作的提示:

Don't create a service instance using new SomeService() . 不要使用new SomeService()创建服务实例。

Instead just add the classes to the providers of bootstrap(.., [Providers]) or the component that should be the root of the scope where a service instance should be shared. 而只是将类添加到bootstrap(.., [Providers])或应该作为应该共享服务实例的作用域的根的组件。

Also add all dependencies (constructor arguments) to the providers list. 还要将所有依赖项(构造函数参数)添加到提供程序列表中。

If all is set up correctly, everywhere where an instance is requested by a constructor argument, an instance with all dependencies automatically resolved is passed in. 如果all设置正确,那么构造函数参数请求实例的任何地方都会传入一个自动解析所有依赖项的实例。

Just configure DI and let it do the work for you. 只需配置DI并让它为您完成工作。

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

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