Vueer
Vueer

Reputation: 1512

How to get access to a foreign Vue3 instance to call a function?

How could I get access to a Vue 3 instance from which I don't know the name of the variable which stores this instance? There I need to call a function from a specific component.

When typing $0.__vue_app__ in the console (Chrome Developer Tools), I see the data of the instance, but how could I use this in my code inside a JS file?

console.log($0) will be undefined of course.

Which way I could go here?

Upvotes: 0

Views: 294

Answers (1)

Max S.
Max S.

Reputation: 1461

One option is to use a worker in 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 уоu can do:

this.postMessage('run_my_function');

Upvotes: 1

Related Questions