我正在测试全新发布的Angular 17与通用SSR和非 destruct 性水化,我注意到一个奇怪的行为.
在我的测试组件中,通过async pipe
Observable
发出了GET和POST请求.
get()
个可观察消费者仅在服务器端触发HTTP请求(在我的浏览器网络选项卡中没有对此请求的XHR调用).在我看来,这似乎是所需的行为,因为Express服务器已经用数据填充了请求的页面)
post()
个可观察的消费者在两端触发HTTP请求.响应已经在从Express生成的HTML中,但是POST请求被客户端第二次触发(我在我的浏览器网络选项卡中看到它).
我在这里错过了什么?
以下是我的代码片段:
export class AppComponent {
getReq$: Observable<any>;
postReq$: Observable<any>;
constructor(
private http: HttpClient,
) {
this.getReq$ = this.get();
this.postReq$ = this.post();
}
get(): Observable<HttpResponse<any>> {
return this.http.get<any>(
'https://dummyjson.com/products/1'
, {});
}
post(): Observable<HttpResponse<any>> {
return this.http.post<any>(
'https://dummyjson.com/products/add'
, {})
}
}
<div *ngIf="getReq$ | async as resp">
<b>get request response</b>
{{resp | json}}
</div>
<div *ngIf="postReq$ | async as resp">
<b>post request response</b>
{{resp | json}}
</div>