javascripthtmlvue.jsvuexpyodide

How to send a message from index.html to Vue.js 3 instance?


So, imagine Vue index.html that also loads some custom script:

<!DOCTYPE html>
<html lang="en">
<head>
    ...
    ...
    <script type="text/javascript">
    languagePluginLoader.then(function () {
        pyodide.loadPackage("someName").then(() => {
            // Send message to Vue that everything is fine
        }).catch((err) => {
            // Send message to Vue that it failed
        })
    })
    </script>
    ...
    ...
</head>
<body>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>

Is there a way to communicate with running Vue instance or/and Vuex from the index.html file? For example, I want to show "Loading..." until the script is fully loaded, etc.

So, any advice will do :)


Solution

  • The solution was the third one - inject script manually through mounted and put all the logic into script.onload part. Google Maps example:

    mounted: function () {
        if (window.google && window.google.maps) {
            this.create_map();
            return;
        }
    
        var self = this;
        var script = document.createElement("script");
        script.onload = function () {
            if (!window.google && !window.google.maps)
                return void (console.error("no google maps script included"));
    
            self.create_map();
        };
    
        script.async = true;
        script.defer = true;
        script.src = "https://maps.googleapis.com/maps/api/js?key=googleapikeyxxxx&callback=initMap";
        document.getElementsByTagName("head")[0].appendChild(script);
    }
    

    Picked the logic from another SO question's answer: https://stackoverflow.com/a/45605316/1598470.