0
我想用另一個環境變量。在我的情況下,test
。原因是我想爲我的集成測試注入一個http攔截器。這個攔截器應該只返回一個含有預定義數據的模擬響應,僅用於測試。如何在Angular中指定自定義環境變量?
有沒有官方的角度?
所以乾脆我想這app.module.ts
:
import { environment } from '../environments/environment';
// ...
export function CustomHttpProvider (backend: XHRBackend, options: RequestOptions) {
return new HttpService(backend, options);
}
export function FakeHttpProvider (backend: XHRBackend, options: RequestOptions) {
return new FakeHttpService(backend, options);
}
const HttpProvider = environment.test ? FakeHttpProvider : CustomHttpProvider;
@NgModule({
imports: [
BrowserModule,
HttpModule,
BrowserAnimationsModule,
// ... my modules + routing here
],
declarations: [AppComponent],
providers: [
{
provide: Http,
useFactory: HttpProvider,
deps: [XHRBackend, RequestOptions]
}
],
bootstrap: [AppComponent]
})
export class AppModule {}
我錯過了將'environment.test.ts'文件添加到'angular-cli.json'中的'environments'下。該博客文章的鏈接顯示了更多信息。感謝您的回答 ;-) –