我需要使用 axios 创建一个用于在 React 项目中加载 API 的进度条,我为此发现了“onDownloadProgress”函数。但我不知道我们是否可以使用它来获取诸如加载百分比之类的信息,或者它是否仅用于文件下载?
所以我不确定我们是否可以通过这个函数获取有关API加载的信息?
我尝试在我的代码中实现这个功能:
componentWillMount() {
axios.get('https://guillaumeduclos.fr/jd-portfolio/wp-json/wp/v2/posts')
.then(response => {
axios.onDownloadProgress = (progressEvent) => {
let percentCompleted = Math.round( (progressEvent.loaded * 100) / progressEvent.total );
console.log(percentCompleted);
}
this.setState({
datas: response.data[0].acf.project_illustration.url,
loading: false
});
})
.catch(error => {
if(error.response) {
console.log(error.responderEnd);
}
});
}
console.log() 不显示。感谢您的帮助。
您需要通过onDownloadProgress
作为一个选项。
由于长度可计算,它将打印出“Infinity”。
这是关于该问题的帖子:通过 xhr 实现 JQuery ajax 进度 https://stackoverflow.com/questions/22502943/jquery-ajax-progress-via-xhr/22504560#22504560
componentWillMount() {
axios.get('https://guillaumeduclos.fr/jd-portfolio/wp-json/wp/v2/posts', {
onDownloadProgress: (progressEvent) => {
let percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total);
console.log(progressEvent.lengthComputable)
console.log(percentCompleted);
}
})
.then((response) => {
this.setState({
datas: response.data[0].acf.project_illustration.url,
loading: false
})
}).catch(error => {
if (error.response) {
console.log(error.responderEnd);
}
});
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)