1

I have this code that works fine in pure html/javascript, but not in vuejs. I have this function named myMethod(). When debugging in the javascript console,I can access it like this: myMethod("toto"). But in vuejs it is only accessible like this: $vm0.myMethod("toto").

I absolutely need it to be accessible like this: myMethod("toto").

Is that even possible in vue?

Thanks in advance.

Lexinho
  • 13
  • 1
  • 5
  • 1
    `window.myMethod = () => {}` and use it anywhere – Alex Mar 27 '19 at 09:57
  • To put it in context, my vue app is just a browser tab in a cellphone app. This cellphone app checks the vue app code for a method named myMethod() and calls it when needed. This app can't call window.myMethod() because it just can't! – Lexinho Mar 27 '19 at 10:06

1 Answers1

0

For non-parent-child relation, then this is the same as this one. Call one method, apparently any method of a component from any other component. Just add a $on function to the $root instance and call form any other component accessing the $root and calling $emit function.

On First component

    ....
    mounted() {
        this.$root.$on('component1', () => {
            // your code goes here
            this.c1method()
        }
    }

and in the second component call the $emit function in $root

    ...
    c2method: function(){
     this.$root.$emit('component1') //like this
    },

It acts more like a socket. Reference here

https://stackoverflow.com/a/50343039/6090215

  • Thanks for the reply, but I think this is working within one vue app, or am I missing something? My vue component method is to be called by another app which is not a vuejs app. Not another component from my vue app. I hope I am being clear... – Lexinho Mar 28 '19 at 09:12