Angular2 RC BaseRequestOption构造函数注入 [英] Angular2 RC BaseRequestOption Constructor Injection

查看:224
本文介绍了Angular2 RC BaseRequestOption构造函数注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我是否缺少一些东西,但注入一个自定义baserequestoptions类的构造函数在Beta 17中对我工作正常,但在移动到RC1后,这种方法似乎不再工作了。



我创建了一个plunkr来说明webapibaseurl现在是未定义的(同样的代码方法,但Beta 17的引用工作):



https://embed.plnkr.co/usOljRDLap9RlLd3RIBd/ p>

任何想法?

解决方案

这里是我使用的自定义选项类:

  import'baseRequestOptions,RequestOptions,RequestOptionsArgs} from'@ angular / http'; 

export class AppRequestOptions extends BaseRequestOptions {
constructor(){
}

merge(options?:RequestOptionsArgs):RequestOptions {
options .url ='https://www.test.org'+ options.url;
return super.merge(options);
}
}

我用这种方式注册:

  bootstrap(App,[
HTTP_PROVIDERS,
provide(RequestOptions,{useClass:AppRequestOptions})
] );

查看此plunkr: https://plnkr.co/edit/MK30JR2qK8aJIGwNqMZ5?p=preview



编辑



似乎在这种类的依赖注入级别有一个问题。我打开了一个问题: https://github.com/angular/angular/issues/8925


I don’t know whether I am missing something but injecting the constructor of a custom baserequestoptions class was working fine for me in Beta 17 but after moving to RC1 this approach doesn’t seem to work any more.

I have created a plunkr to illustrate that the webapibaseurl now comes through as undefined (the same code approach but with Beta 17 references worked):

https://embed.plnkr.co/usOljRDLap9RlLd3RIBd/

Any ideas?

解决方案

This still works for me. Here is the custom option class I used:

import {BaseRequestOptions, RequestOptions, RequestOptionsArgs} from '@angular/http';

export class AppRequestOptions extends BaseRequestOptions {
  constructor() {
  }

  merge(options?:RequestOptionsArgs):RequestOptions {
    options.url = 'https://www.test.org' + options.url;
    return super.merge(options);
  }
}

and I register it this way:

bootstrap(App, [
  HTTP_PROVIDERS,
  provide(RequestOptions, {useClass: AppRequestOptions})
]);

See this plunkr: https://plnkr.co/edit/MK30JR2qK8aJIGwNqMZ5?p=preview.

Edit

It seems that there is a problem at the level of dependency injection for such class. I opened an issue: https://github.com/angular/angular/issues/8925.

这篇关于Angular2 RC BaseRequestOption构造函数注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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