gpt4 book ai didi

php - 当 SPA 中的路由发生变化时,http 请求会发生什么情况

转载 作者:太空宇宙 更新时间:2023-11-03 23:17:23 24 4
gpt4 key购买 nike

让我们想象一下请求正在加载并且页面(组件)更改了正在加载的请求会发生什么?

请考虑使用前端SPA(单页应用程序)的情况,例如使用angular.js、angular、react等的项目。 &&

后端会发生什么?

最佳答案

根据您使用的请求库,有一些选项。但正如您所说,它是一个单页面应用程序,您可以卸载该页面或组件。

示例:

使用 React,生命周期方法之一是 ComponentWillUnmount,假设您在 ComponentDidMount 中发送请求,并且在请求完成之前用户离开页面。您可以在ComponentWillUnmount中取消请求。

您可以使用几乎所有 http 库取消请求,特别是带有 rxjs 的 redux-oberavble 是一个非常好的选择。

import { ajax } from 'rxjs/ajax';

const fetchUserEpic = action$ => action$.pipe(
ofType(FETCH_USER),
mergeMap(action => ajax.getJSON(`/api/users/${action.payload}`).pipe(
map(response => fetchUserFulfilled(response)),
takeUntil(action$.pipe(
ofType(FETCH_USER_CANCELLED)
))
))
);

当用户按下按钮或加载页面时,您可以启动一个操作来获取用户数据,直到完成或启动 fetch_user_cancelled 操作,例如在 componentWillUnmount 中。

希望能帮到你:)

关于php - 当 SPA 中的路由发生变化时,http 请求会发生什么情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53680783/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com