Jitendra
Jitendra

Reputation: 531

How do I call a Vue method into a plain javascript function?

Vue app code:

var app = new Vue({
    el: "#APP",
    data: {some data},
    methods: {
            some_method: function() {
                   ......
            }
});

some_js_func = function() {
       "How do I call 'some_method' to here"
};
some_js_func();

I tried by calling app.some_method(), but it's not working.

Upvotes: 5

Views: 3987

Answers (1)

AndrewL64
AndrewL64

Reputation: 16311

With pure JavaScript, you can access the some_method function via the $options property like this:

var app = new Vue({
    methods: {
            some_method: function() {
                   alert("hello");
            }}
});

someJSfunc = function() {
       app.$options.methods.some_method();
};

someJSfunc();
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

Upvotes: 4

Related Questions