问题描述: 当使用Angular的HttpClientModule发送HTTP请求时,页面初始化加载时没有发出HTTP请求。
解决方法:
import { HttpClientModule } from '@angular/common/http';
@NgModule({
imports: [
HttpClientModule,
...
],
...
})
export class AppModule { }
import { HttpClient } from '@angular/common/http';
export class MyComponent {
constructor(private http: HttpClient) {
// 发送HTTP请求的代码
}
}
import { HttpClient } from '@angular/common/http';
export class MyComponent {
constructor(private http: HttpClient) { }
ngOnInit() {
this.http.get('https://api.example.com/data')
.subscribe((response) => {
// 处理HTTP响应
});
}
}
{{ data }}
以上是解决Angular HttpClientModule在页面加载时没有发出HTTP请求的常见方法。请根据具体情况进行调试和调整。