axios vue 加载效果动画_Vue.js+axios+element ui 实现全局loading加载示例

Vue.js+axios+element ui 实现全局loading加载示例

发布于 2020-2-27|

复制链接

分享一篇关于vue+axios+element ui 实现全局loading加载示例,具有很好的参考价值,希望对大家有所帮助。一起跟随小妖过来看看吧

实现全局loading加载分析需求,我们只需要在请求发起的时候开始loading,响应结束的时候关闭loading,就这么简单 对不对?

```javascript

import axios from 'axios';

import { Message, Loading } from 'element-ui';

import Cookies from 'js-cookie';

import router from '@/router/index'

let loading //定义loading变量

function startLoading() { //使用Element loading-start 方法

loading = Loading.service({

lock: true,

text: '加载中……',

background: 'rgba(0, 0, 0, 0.7)'

})

}

function endLoading() { //使用Element loading-close 方法

loading.close()

}

//那么 showFullScreenLoading() tryHideFullScreenLoading() 要干的事儿就是将同一时刻的请求合并。

//声明一个变量 needLoadingRequestCount,每次调用showFullScreenLoading方法 needLoadingRequestCount + 1。

//调用tryHideFullScreenLoading()方法,needLoadingRequestCount - 1。needLoadingRequestCount为 0 时,结束 loading。

let needLoadingRequestCount = 0

export function showFullScreenLoading() {

if (needLoadingRequestCount === 0) {

startLoading()

}

needLoadingRequestCount++

}

export function tryHideFullScreenLoading() {

if (needLoadingRequestCount {

var token = ''

if(typeof Cookies.get('user') === 'undefined'){

//此时为空

}else {

token = JSON.parse(Cookies.get('user')).token

}//注意使用的时候需要引入cookie方法,推荐js-cookie

config.data = JSON.stringify(config.data);

config.headers = {

'Content-Type':'application/json'

}

if(token != ''){

config.headers.token = token;

}

showFullScreenLoading()

return config;

},

error => {

return Promise.reject(err);

}

);

//http response 拦截器

axios.interceptors.response.use(

response => {

//当返回信息为未登录或者登录失效的时候重定向为登录页面

if(response.data.code == 'W_100004' || response.data.message == '用户未登录或登录超时,请登录!'){

router.push({

path:"/",

querry:{redirect:router.currentRoute.fullPath}//从哪个页面跳转

})

}

tryHideFullScreenLoading()

return response;

},

error => {

return Promise.reject(error)

}

)

```

Vue使用Axios进行数据请求时,我们可以使用loading动画来增加页面的交互体验。具体实现如下: 1. 安装`axios`和`vue-spinner`依赖: ```bash npm install axios vue-spinner --save ``` 2. 在需要使用的组件引入`axios`和`vue-spinner`: ```javascript import axios from 'axios'; import Spinner from 'vue-spinner/src/ScaleLoader.vue'; ``` 3. 在`data`添加`loading`属性,并将其初始化为`false`: ```javascript data() { return { loading: false, // ... } } ``` 4. 在需要使用loading动画的方法,首先将`loading`设为`true`,然后在请求完成后将其设为`false`: ```javascript methods: { fetchData() { this.loading = true; axios.get('/api/data').then(response => { // 处理数据 this.loading = false; }).catch(error => { console.log(error); this.loading = false; }); } } ``` 5. 在模板使用`vue-spinner`来显示loading动画: ```html <template> <div> <spinner v-if="loading" color="#41b883" :size="100" :margin="10"></spinner> <!-- 显示数据的内容 --> </div> </template> <script> import axios from 'axios'; import Spinner from 'vue-spinner/src/ScaleLoader.vue'; export default { components: { Spinner }, data() { return { loading: false, // ... } }, methods: { fetchData() { this.loading = true; axios.get('/api/data').then(response => { // 处理数据 this.loading = false; }).catch(error => { console.log(error); this.loading = false; }); } } } </script> ``` 这样,在数据请求时,loading动画会显示,请求完成后动画消失。这样可以提高用户的交互体验,让页面更加友好。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值