Answer the question
In order to leave comments, you need to log in
How to determine if a Vue plugin is registered?
The scripts will have to work in an unknown environment, where Vue and some other libraries may or may not already be loaded.
To do this, I created the following initialization skeleton:
init: function (conteinerId,tags) {
tags = tags || [];
this._checkDependencies().then(() => {
console.log('Go')
});
},
_checkDependencies: function () {
var promise = new Promise(function(resolve, reject) {resolve(true);});
if (typeof Vue == 'undefined')
promise = promise.then(script => this._loadScript('//cdn.jsdelivr.net/npm/[email protected]/dist/vue.js'));
if (typeof axios == 'undefined')
promise = promise.then(script => this._loadScript("//unpkg.com/axios/dist/axios.min.js"))
promise = promise.then(script => this._loadScript("//unpkg.com/[email protected]/dist/vue-axios.min.js"))
return promise;
},
_loadScript: function (src) {
return new Promise(function(resolve, reject) {
let script = document.createElement('script');
script.src = src;
script.type = 'text/javascript';
script.onload = () => resolve(script);
script.onerror = () => reject(new Error(`Ошибка загрузки скрипта ${src}`));
document.head.append(script);
});
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question