Home  >  Q&A  >  body text

How to access external Vue3 instance to call functions?

<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粉141911244P粉141911244413 days ago450

reply all(1)I'll reply

  • P粉714780768

    P粉7147807682023-09-03 10:01:15

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

    reply
    0
  • Cancelreply