How to access external Vue3 instance to call functions?
P粉141911244
P粉141911244 2023-09-02 14:36:10
0
1
497
<p>How do I access a Vue 3 instance if I don't know the name of the variable that stores the instance? I need to call a function from a specific component. </p> <p>When typing <code>$0.__vue_app__</code> in the console (Chrome Developer Tools), I see the instance's data, but how do I use it in code inside the JS file? </p> <p><code>console.log($0)</code> is of course undefined. </p> <p>Which way can I take to get here? </p>
P粉141911244
P粉141911244

reply all(1)
P粉714780768

One option is in workerCode>mounted()such as:

methods: {
    myFunction(){
        console.log('It works!');
    }
},
mounted() {
    window["myWorker"] = new Worker("./myWorker.js");

    window["myWorker"].onmessage = function(event) {
        if (event && event.data === 'run_my_function') {
            this.myFunction();
        }
    }
}

In myWorker.js you can do this:

this.postMessage('run_my_function');
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template