0

In my main.js file I set vue up like so:

window.Vue = require('vue');

Vue.component('my-component', require('./components/MyComponent.vue'));

const app = new Vue({
    el: '#app',
});

Also in my main.js file I have a function:

function test() {
    //do something
}

How can I call the test method from inside a vue component? I know this is not an ideal set up but I'm working with legacy stuff.

4
  • 1
    Pass it as a prop. Make it globally accessible. Attache to prototype. Plenty of ways to do it. Commented Dec 19, 2017 at 15:05
  • 1
    If that function has been declared globally, you can simply call it from inside your Vue component. Commented Dec 19, 2017 at 15:07
  • How do I declare it globally? Commented Dec 19, 2017 at 15:17
  • I beleive I have declared test() globally, yet the vue component says its still not defined. Commented Dec 19, 2017 at 15:24

1 Answer 1

1

I guess its really easy thing :)

inside your main file where you'r having that function

just declare it globally

window.test = function() {
    //do something
}

now you can call this function anywhere from your code / and from any file / from any component :)

window.test();

if you need anything else please comment.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.