Chris
Chris

Reputation: 3129

When using a Vue component multiple times in a single page application is the javascript loaded multiple times?

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?

Upvotes: 0

Views: 2989

Answers (1)

tony19
tony19

Reputation: 138356

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

Upvotes: 3

Related Questions