Skip to content
This repository was archived by the owner on Feb 26, 2020. It is now read-only.

Commit 2cf30c0

Browse files
committed
添加新的配置项
enableLoadingBar 全局设置是否启用loadingbar,默认启用 $hideLoadingBar 设置当前请求是否显示loadingbar,默认显示
1 parent 44af8a4 commit 2cf30c0

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,26 @@ Vue.use(vueLoadingBar);
4747

4848
## config
4949

50+
###### loadingBarDelay ```default 300ms```
51+
5052
```javascript
5153
// 设置请求超过多少毫秒才显示,默认300毫秒
5254
new Vue({...}).loadingBarDelay = 100;
5355
```
56+
57+
###### enableLoadingBar ```default true```
58+
59+
```javascript
60+
// 设置全局禁用loadingbar
61+
new Vue().enableLoadingBar = false;
62+
```
63+
64+
###### $hideLoadingBar ```default false```
65+
66+
当前请求禁用loadingbar(不影响其他请求)。只需要在请求的config部分(如[axios request](https://github.com/mzabriskie/axios#request-method-aliases))设置$hideLoadingBar为true即可
67+
68+
```javascript
69+
// 设置此次请求不显示loadingbar
70+
http.get(url, {$hideLoadingBar: true}).then(...); // get
71+
http.post(url, data, {$hideLoadingBar: true}).then(...); // post
72+
```

dist/loadingbar.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/example.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
});
2424
function _click() {
2525
var _http = Vue.http || axios;
26-
_http.get('./test').then(function (data) {
26+
// _http.get('./test', {$hideLoadingBar: true}).then(function (data) {
27+
// console.log(data);
28+
// });
29+
_http.post('./test', {}, {$hideLoadingBar: true}).then(function (data) {
2730
console.log(data);
2831
});
2932
}

src/loadingbar.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
var delayTimeoutId = 0;
1313
var loadingEle = document.createElement('div');
1414
var loadingBarDelay = 300;
15+
var enable = true;
1516

1617
/**
1718
* 隐藏loading框
@@ -27,18 +28,16 @@
2728

2829
/**
2930
* 延迟显示loading框
30-
* @param request
31-
* @param next
3231
* @private
3332
*/
34-
function _showLoading(request, next) {
33+
function _showLoading() {
34+
if (!enable)return false;
3535
if (requestCount === 0) {
3636
delayTimeoutId = setTimeout(function () {
3737
loadingEle.classList.add('show');
3838
}, loadingBarDelay); // 延迟显示,当请求响应时间过快时不需要显示loading
3939
}
4040
requestCount++;
41-
next && next(_hideLoading); // 如果是vue-resource则有next
4241
}
4342

4443
/**
@@ -55,6 +54,14 @@
5554
set: function (_loadingBarDelay) {
5655
loadingBarDelay = Number(_loadingBarDelay);
5756
}
57+
},
58+
enableLoadingBar: {
59+
get: function () {
60+
return enable;
61+
},
62+
set: function (enableLoadingBar) {
63+
enable = Boolean(enableLoadingBar);
64+
}
5865
}
5966
});
6067
if (!document.getElementById('LoadingBar')) {
@@ -67,12 +74,16 @@
6774
* 判断使用的是vue-resource还是axios
6875
*/
6976
if (Vue.http) {
70-
Vue.http.interceptors.push(_showLoading);
77+
// vue-resource
78+
Vue.http.interceptors.push(function (request, next) {
79+
!request.$hideLoadingBar && _showLoading();
80+
next(_hideLoading);
81+
});
7182
} else if (Vue.axios || Vue.$axios || axios) {
7283
// axios 支持不绑定到Vue属性上或者绑定为Vue.axios、Vue.$axios
7384
var _axios = Vue.axios || Vue.$axios || axios;
7485
_axios.interceptors.request.use(function (request) {
75-
_showLoading();
86+
!request.$hideLoadingBar && _showLoading();
7687
return request;
7788
}, function (err) {
7889
_hideLoading();

0 commit comments

Comments
 (0)