[英]HttpClient Angular 5 does not send request
我在Angular 5中遇到HttpClient的問題.HttpClient不會在兩個指定的組件上發送任何請求(我在控制台中看不到任何xhr日志)。 在其他組件上一切都很好。
從組件A調用ApiService POST方法(自定義服務,其作用類似於HttpClient的包裝),但是當我從組件B調用此方法時,HttpClient似乎被凍結。
我的應用程序中有許多組件使用ApiService。 一切都很好。 我不知道出了什么問題。
---回應
ApiService.ts
@Injectable()
export class ApiService
{
private errorListeners : Map<string, Array<(details ?: any) => any>> =
new Map<string, Array<(details ?: any) => any>>();
public constructor(private http: HttpClient)
{
}
public post<T>(path : string, data : any, urlParams : any = null) : Observable<any>
{
return this.http.post<T>(`${environment.api.path}${path}`, data, {
params: urlParams
}).catch(this.catchErrors()).map(response => {
if (response['Error']){
throw response['Error'];
}
return response;
});
}
}
- 零件
@Component({
selector: 'login-register-component',
templateUrl: './register.component.html',
styleUrls: [
'./../../assets/main/css/pages/login.css'
]
})
export class RegisterComponent implements OnInit, OnDestroy
{
public constructor(private route: ActivatedRoute,
private router: Router,
private userService : UserService,
private apiService: ApiService
)
{
this.apiService.post('/some-endpoint', null, {}).subscribe(res => {
console.log(res);
});
}
即使我直接將HttpClient注入Component,HttpClient也不起作用
- 同一模塊示例中的其他組件調用:(可行)
public loginTraditionalMethod(emailAddress : string, plainPassword : string)
{
this.apiService.post('/auth/email', {
email: emailAddress,
password: plainPassword
}, {}).subscribe(res => {
console.log(res);
})
}
在訂閱http.get()
之后,我遇到了同樣的問題,沒有xhr請求。 這是一個忘記密碼功能的請求,因此我沒有連接到該應用程序。
請求被一個http令牌攔截器攔截,如果沒有檢測到會話,則該攔截器返回一個空的Observable。
永遠不知道,這可能對某人有幫助......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.