我在 Angular 5 中使用 HttpClient 遇到了问题。HttpClient 在两个特定的组件上不发送任何请求(控制台中没有看到任何 xhr 日志)。但是,在其他组件中一切正常。
我从组件 A 中调用 ApiService 的 POST 方法(一个类似于 HttpClient 的包装器自定义服务),但当我从组件 B 中调用此方法时,HttpClient 似乎被冻结了。
我的应用程序中有许多使用 ApiService 的组件,所有东西都被注入了,但我不知道出了什么问题。
--- 回复
ApiService.ts
我从组件 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注入到组件中,它也无法正常工作。
--在同一模块中的其他组件示例调用:(它可以正常工作)
public loginTraditionalMethod(emailAddress : string, plainPassword : string)
{
this.apiService.post('/auth/email', {
email: emailAddress,
password: plainPassword
}, {}).subscribe(res => {
console.log(res);
})
}