4
我曾經使用HttpModule來觸發ajax調用,並有一個模型後端來捕獲前端開發中的每個請求。 現在我使用HttpClientModule,因爲我想使用攔截器功能。但是基於Http的我的樣機後臺無法正常工作。 我該如何修改我的樣機後臺,使其捕獲來自HttpCleint的呼叫。Angular 4.3具有MockBackend的HttpClient
假backend.ts
import { Http, BaseRequestOptions, Response, ResponseOptions, RequestMethod } from '@angular/http';
import { MockBackend, MockConnection } from '@angular/http/testing';
import { environment } from '../../environments/environment';
export function fakeBackendFactory(backend: MockBackend, options:
BaseRequestOptions) {
// configure fake backend
backend.connections.subscribe((connection: MockConnection) => {
let testUser = { username: 'test', password: 'test', firstName: 'Test', lastName: 'User' };
// wrap in timeout to simulate server api call
setTimeout(() => {
//
if (connection.request.url.includes('/daquery/ws/setup') && connection.request.method === RequestMethod.Get) {
...
}
return new Http(backend, options);
}