简体   繁体   English

Angular 2依赖注入 - 服务注入某些组件但不是全部

[英]Angular 2 dependency injection - A service is injected in some components but not in all

I am working to an Angular 2 application which has only one module with several services registered as providers at bootstrap. 我正在开发一个Angular 2应用程序,该应用程序只有一个模块,其中有几个服务在bootstrap中注册为提供者。 The application has also several components in the same module which are using these services. 该应用程序还在同一模块中使用这些服务的几个组件。

All components are receiving the services using DI (services are declared as parameters in construtor). 所有组件都使用DI接收服务(服务在construtor中声明为参数)。

The problem that I am facing is: the same service is correctly injected in some components but in other no. 我面临的问题是:在某些组件中正确注入了相同的服务,但在其他组件中没有。

I checked the Injector object for the components where the injection fails and the services instances are within the Injector object. 我检查了Injector对象中注入失败的组件以及服务实例是否在Injector对象中。 What it fails is the searching with the Injector.get() method. 失败的是使用Injector.get()方法进行搜索。 The token provided as parameter is different from the token stored in the Injector dictionary. 作为参数提供的令牌与存储在Injector字典中的令牌不同。 I am not able to find out why because I am using the class name of the service and the token is automatically generated by the Javascript engine from this class name. 我无法找到原因,因为我使用的是服务的类名,而Javascript引擎会自动从此类名生成令牌。

I tried to use OpaqueTokens to control the injection and, again, even if I am using the same opaque token, when Injector.get() method is called, the token provided by argument is considered different from the token stored by injector. 我尝试使用OpaqueTokens来控制注入,并且,即使我使用相同的不透明令牌,当调用Injector.get()方法时,参数提供的令牌被认为与注入器存储的令牌不同。

A short fragment from the code looks like 代码中的一个简短片段看起来像

myservice.service.ts myservice.service.ts

import { OpaqueToken } from '@angular/core'

export const BACKEND = new OpaqueToken('backend');

app.module.ts app.module.ts

import { BACKEND } from 'myservice.service'
...
providers: [{ provide: BACKEND, useValue: "TestValue" }]

mycomponent.component.ts mycomponent.component.ts

import { Component, Inject } from '@angular/core'
import { BACKEND } from 'myservice.service'

@Component(
{
selector:'mycomponent'
templateUrl: ...
})
export class MyComponent
{
  constructor(@Inject(BACKEND) config: string) {
    console.info(JSON.stringify(config));
  }
}

This injection with BACKEND token works for some components but for other not (the empty {} object is injected). 这种带有BACKEND令牌的注入适用于某些组件,但对于其他组件则不适用(注入空{}对象)。 I tried to debug the Javascript code in browser and it looks like the Injector is called with a different token than the token from dictionary (they are looking the same, with the same name 'backend' but still different and === returns false). 我试图在浏览器中调试Javascript代码,它看起来像是使用与字典中的令牌不同的令牌调用Injector(它们看起来相同,具有相同的名称'后端'但仍然不同而且===返回false) 。

Note: I am using Angular 2.4.2, Typescript 2.1 with 'commonjs' modules and 'node' module resolution. 注意:我使用的是Angular 2.4.2,Typescript 2.1,带有'commonjs'模块和'node'模块解析。 systemjs is used as module loader. systemjs用作模块加载器。

Please, can you provide some ideeas about how to fix this? 请你能提供一些关于如何解决这个问题的想法吗?

Thanks, 谢谢,

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

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