Vue实现loading加载动画
1.在main.js里引入axios
import axios from "@/http/index.js"
2. 在vuex中设置状态
state: {
isLoading: false
},
3.配置拦截器
import axios from 'axios'
import store from '@/store'
var instance =axios.create({
baseURL:'',
timeout:5000
})
instance.interceptors.request.use((config)=>{
store.state.isLoading=true
return config
},error=>{
return Promise.reject(error)
})
instance.interceptors.response.use(response => {
if(response.status === 200) {
store.state.isLoading=false
return Promise.resolve(response)
}
}, error => {
return Promise.reject(error)
})
export default instance
4.在app.vue中引入
<Loading v-if="isLoading" />
import { mapState } from "vuex";
export default {
computed: {
...mapState(["isLoading"])
},
}