A
A
Alexander2021-03-30 19:55:34
Vue.js
Alexander, 2021-03-30 19:55:34

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);
        });
    }

And everything is fine if vue-axios is not loaded yet on the page, but if it is, the error Uncaught TypeError: Cannot redefine property: axios crashes due to the fact that I reload it. How to check if vue-axios is already loaded? Or that it is at least registered with Vue?

I tried to look at the plugin code, but it's not very clear to me - I don't see that it creates any global variables for the presence of which you can navigate.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex, 2021-03-30
Madzhugin @Suntechnic

Doesn't the plugin add something like Vue.axios?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question