在扩展BaseRequestOptions时,注入的依赖关系是未定义的 [英] Injected dependency is undefined when extending BaseRequestOptions

查看:187
本文介绍了在扩展BaseRequestOptions时,注入的依赖关系是未定义的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Angular2中扩展了 BaseRequestOptions ,为每个请求添加标题。我还有一个 Config 类,它根据域提供键/值对,我需要注入到我的派生类中:

I am extending BaseRequestOptions in Angular2 to add headers for each request. I also have a Config class that supplies key/value pairs based on the domain, which I need to inject into my derived class:

import { BaseRequestOptions } from '@angular/http';
import { Config } from '../../config/configuration';

export class DefaultRequestOptions extends BaseRequestOptions {
  constructor(private config: Config) {
    super();

    Object.keys(this.config.api.headers).map((key) => {
      this.headers.append(key, this.config.api.headers[key]);
    });
  }
}

在我的模块中,我将提供者指定为:

In my module, I am specifying the provider as such:

@NgModule({
  . . .,
  providers: [
    . . .,
    { provide: RequestOptions, useClass: DefaultRequestOptions }
  ]
})
. . .

我遇到的问题是 this.config DefaultRequestOptions 未定义。我也使用 Config 类作为其他类中注入的依赖关系,所以我相信它按预期工作,如果我手动设置 this.headers 一切正常。

The problem I'm having is that this.config is undefined in DefaultRequestOptions. I am also using the Config class as an injected dependency in other classes, so I'm confident it's working as expected, and if I manually set the values for this.headers everything works fine.

我做错了什么会导致配置在 DefaultRequestOptions ?

What am I doing wrong that would cause the config to be undefined in DefaultRequestOptions?

推荐答案

提供者类不需要 @Injectable ) ,除非需要注入构造函数参数。在你的情况下。这就是为什么Angular建议永远在您的供应商上使用它,无论是否需要注射。也许是因为这个确切的原因,当你需要时,你忘记了什么。

Provider classes don't require @Injectable() unless they require constructor parameters to be injected. In your case it does. This is why Angular recommends to always use it on your providers, whether or not you require injections. Maybe for this exact reason, where you forget, when it's needed.

@Injectable()
export class DefaultRequestOptions extends BaseRequestOptions {






经过测试

似乎这还是不行。我看到这个问题是扩展一个已经使用 @Injectable()(其中 BaseRequestOptions )。在这种情况下,调用父构造函数而不是扩展类'。如果你扩展了 RequestOptions (不使用 @Injectable()进行装饰),那么它将工作

It seems this still doesn't work. I've seen this problem is a result of extending a class that already uses @Injectable() (which BaseRequestOptions does). In this case, the parent constructor is called instead of the extending class'. If you extend RequestOptions instead (which is not decorated with @Injectable()) then it would work

@Injectable()
class DefaultRequestOptions extends RequestOptions {
  constructor(public config: Config) {
    super({method: RequestMethod.Get, headers: new Headers()})

    this.headers.append('data', this.config.data);
  }
}

注意 super() 通话。这就是 BaseRequestOptions 确实

Notice the super() call. This is all that BaseRequestOptions does.

如果你想保持目前的方式,使用 BaseRequestOptions ,那么你可以使用工厂,而不是让Angular创建它

If you wanted to keep it the way it currently is, using BaseRequestOptions, then you could use a factory, instead of letting Angular create it

{
  provide: RequestOptions,
  deps: [ Config ],
  useFactory: (config: Config) => {
    return new DefaultRequestOptions(config);
  }
}

这篇关于在扩展BaseRequestOptions时,注入的依赖关系是未定义的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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