2016-05-31 190 views

回答

0

這仍然適用於我。這是自定義的選項類我用:

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

+1

感謝您回覆我。選項類本身很好,並且對請求起作用,但是對於我來說關鍵的一點是要爲'undefined'注入'webApiBaseUrl'(或者其他任何其他的東西)。 – Monkeeman69

+0

@ Monkeeman69 Wouah,你完全正確。看起來,依賴注入對擴展'BaseRequestOptions'的類不起作用...而注入('webApiBaseUrl')可能在其他地方。我認爲這是一個錯誤/迴歸。我會爲此打開一個錯誤。 –

+0

這是我打開的問題:https://github.com/angular/angular/issues/8925。 –

4

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沒有工作。

Plunker example