0

When using a Vue component multiple times in a single page application is the javascript loaded multiple times and do the methods within the script get called multiple times for a single method call?

IE if I use a component 3 times, and when i call method a, will that be called 3 times or just once?

1 Answer 1

3

The component's lifecycle callbacks are invoked in each component instance. If, for example, the created() callback contained a console.log('hello world'), and you've added three of those components to the document, you would see three logs in the console (one from each instance).

On the other hand, event listeners (such as button-click handlers) would only be invoked in the instance from which the event occurred. Say, your component contained a button that had a click-event handler calling console.log(). Clicking the button in one component triggers the handler only in that component, and not other instances of the component.

demo

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

2 Comments

Thanks for the explanation and example, its appreciated. I do have another question, which is probably off topic but... Can components access functions from an external file?
No problem. Yes, they can.

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.