-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathindex.js
48 lines (41 loc) · 1.34 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* eslint-env browser */
const LoadScript = {
install: function (Vue) {
Vue.loadScript = Vue.prototype.$loadScript = function (src) { // eslint-disable-line no-param-reassign
return new Promise(function (resolve, reject) {
let shouldAppend = false;
let el = document.querySelector('script[src="' + src + '"]');
if (!el) {
el = document.createElement('script');
el.type = 'text/javascript';
el.async = true;
el.src = src;
shouldAppend = true;
}
else if (el.hasAttribute('data-loaded')) {
resolve(el);
return;
}
el.addEventListener('error', reject);
el.addEventListener('abort', reject);
el.addEventListener('load', function loadScriptHandler() {
el.setAttribute('data-loaded', true);
resolve(el);
});
if (shouldAppend) document.head.appendChild(el);
});
};
Vue.unloadScript = Vue.prototype.$unloadScript = function (src) { // eslint-disable-line no-param-reassign
return new Promise(function (resolve, reject) {
const el = document.querySelector('script[src="' + src + '"]');
if (!el) {
reject();
return;
}
document.head.removeChild(el);
resolve();
});
};
},
};
export default LoadScript;