Lia*_*odi 4 javascript interceptor reactjs axios
我在React项目中使用axios进行API调用,我想在axios拦截器的api调用的请求和响应之间全局添加加载或旋转效果,这是我的拦截器的代码。
import Axios from 'axios'
Axios.interceptors.request.use(function (config) {
// spinning start to show
const token = window.localStorage.token;
if (token) {
config.headers.Authorization = `token ${token}`
}
return config
}, function (error) {
return Promise.reject(error);
});
Axios.interceptors.response.use(function (response) {
// spinning hide
return response;
}, function (error) {
return Promise.reject(error);
});
Run Code Online (Sandbox Code Playgroud)
也许您可以采用一种更简单的方法,在您的应用程序正忙于通过axios加载数据时显示全屏加载消息?
例如,您可以在代码/项目中添加以下内容,以在axio请求期间在屏幕上显示全局“加载消息”:
CSS:
/* Define css class for global loading message to cover
screen during axios operations */
.loading-indicator:before {
content: '';
background: #000000cc;
position: fixed;
width: 100%;
height: 100%;
top: 0;
left: 0;
z-index: 1000;
}
.loading-indicator:after {
content: 'Loading';
position: fixed;
width: 100%;
top: 50%;
left: 0;
z-index: 1001;
color:white;
text-align:center;
font-weight:bold;
font-size:1.5rem;
}
Run Code Online (Sandbox Code Playgroud)
Javascript:
Axios.interceptors.request.use(function (config) {
// spinning start to show
// UPDATE: Add this code to show global loading indicator
document.body.classList.add('loading-indicator');
const token = window.localStorage.token;
if (token) {
config.headers.Authorization = `token ${token}`
}
return config
}, function (error) {
return Promise.reject(error);
});
Axios.interceptors.response.use(function (response) {
// spinning hide
// UPDATE: Add this code to hide global loading indicator
document.body.classList.remove('loading-indicator');
return response;
}, function (error) {
return Promise.reject(error);
});
Run Code Online (Sandbox Code Playgroud)
使用这种方法,您甚至可以使用CSS3动画,用动画微调器或类似的东西替换“正在加载”消息-希望这会有所帮助!
您可以在更高级别的组件中设置axios拦截器,例如 App. 您可以使用“SHOW_LOADER”和“HIDE_LOADER”操作类型在减速器中定义加载状态。这些拦截器将在通过axios发送和接收请求之前分派相应的操作,更新存储中的加载状态,您可以通过它呈现 Loader 组件。
希望这能回答您的问题。
应用程序组件
import React from 'react';
import axios from 'axios'
import { connect } from 'react-redux';
import { loading } from '../actions'
import Loader from './Loader'
class App extends React.Component{
componentWillMount(){
const self = this
axios.interceptors.request.use(function (config) {
// spinning start to show
self.props.loading(true)
return config
}, function (error) {
return Promise.reject(error);
});
axios.interceptors.response.use(function (response) {
// spinning hide
self.props.loading(false)
return response;
}, function (error) {
return Promise.reject(error);
});
}
render(){
return (
<div>
{ this.props.loader ? <Loader /> : null }
{/*
Your other components
*/}
</div>
)
}
}
const mapStateToProps = (state)=>{
return {
loader: state.loader
}
}
export default connect(mapStateToProps,{
loading
})(App);
Run Code Online (Sandbox Code Playgroud)
装载机减速机
const loader = (state = false, action) => {
switch (action.type) {
case "SHOW_LOADER":
return action.data;
break;
case "HIDE_LOADER":
return action.data;
break;
default:
return state;
}
}
export default loader;
Run Code Online (Sandbox Code Playgroud)
行动
export const loading = (bool)=>{
return bool ? {
type:"SHOW_LOADER",
data:bool
}: {
type: "HIDE_LOADER",
data: bool
}
}
Run Code Online (Sandbox Code Playgroud)