我不知道我是否缺少一些東西,但注入一個自定義baserequestoptions類的構造函數在Beta 17中對我來說工作正常,但在移動到RC1後,此方法似乎不再適用。Angular2 RC BaseRequestOption構造函數注入
我創建了一個plunkr以表明webapibaseurl現在來通過爲未定義(相同的代碼的方法,但有Beta版17所引用工作):
https://embed.plnkr.co/usOljRDLap9RlLd3RIBd/
任何想法?
我不知道我是否缺少一些東西,但注入一個自定義baserequestoptions類的構造函數在Beta 17中對我來說工作正常,但在移動到RC1後,此方法似乎不再適用。Angular2 RC BaseRequestOption構造函數注入
我創建了一個plunkr以表明webapibaseurl現在來通過爲未定義(相同的代碼的方法,但有Beta版17所引用工作):
https://embed.plnkr.co/usOljRDLap9RlLd3RIBd/
任何想法?
這仍然適用於我。這是自定義的選項類我用:
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。
從RequestOptions
延伸,而不是從BaseRequestOptions
使其成爲我
@Injectable()
export class AppRequestOptions extends RequestOptions {
constructor(@Inject('webApiBaseUrl') private webApiBaseUrl:string) {
super({method: RequestMethod.Get, headers: new Headers()});
console.log('webApiBaseUrl', webApiBaseUrl);
}
merge(options?:RequestOptionsArgs):RequestOptions {
options.url = this.webApiBaseUrl + options.url;
console.log('merge - options.url = '+options.url);
return super.merge(options);
}
}
工作,否則一些未知的原因注入@Inject('webApiBaseUrl') private webApiBaseUrl:string
沒有工作。
感謝您回覆我。選項類本身很好,並且對請求起作用,但是對於我來說關鍵的一點是要爲'undefined'注入'webApiBaseUrl'(或者其他任何其他的東西)。 – Monkeeman69
@ Monkeeman69 Wouah,你完全正確。看起來,依賴注入對擴展'BaseRequestOptions'的類不起作用...而注入('webApiBaseUrl')可能在其他地方。我認爲這是一個錯誤/迴歸。我會爲此打開一個錯誤。 –
這是我打開的問題:https://github.com/angular/angular/issues/8925。 –