Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between vue instance and vue component?

Tags:

vue.js

I'm new to vue js and have some questions when learning it.

I'm now a little confused about the relationship between its instance and component. As far as I learned, every app build by vue should only have one instance and one instance only, it can have as many components as you like if needed. But recently I've seen a demo, and in that demo it has more than one instance.

So my question is, is that ok to have multiple intances in one app( the demo code works fine, but is that the correct way)? What's the best practice to use instance and component in vue app?

like image 791
ericchan1336 Avatar asked Jul 12 '16 01:07

ericchan1336


People also ask

What is Vue component instance?

The object returned from the data option, made reactive by the component. The component instance proxies access to the properties on its data object. Type. ts interface ComponentPublicInstance { $data: object }

What is the difference between views and components in Vue?

The key difference is that some Vue components act as Views for routing. The components located under src/components are less likely to be used in a route whereas components located under src/views will be used by at least one route.

What is type of Vue instance?

A Vue instance is essentially a ViewModel as defined in the MVVM pattern, hence the variable name vm you will see throughout the docs. When you instantiate a Vue instance, you need to pass in an options object which can contain options for data, template, element to mount on, methods, lifecycle callbacks and more.


2 Answers

It's ok to have two instances in the same project, however, you probably don't want to do that.

A good scenario is to have one main instance to control your app, specially if you are creating a Single Page Application (SPA). Then use as many components as you want.

Components are a great way to reuse code and keep it organized, and, with Vue.js, is very easy to communicate between your components and your "main" Vue instance.

like image 68
crabbly Avatar answered Sep 18 '22 09:09

crabbly


It depends very much on your situation.

I work on a large web project which is not an SPA. We have a Vue instance for each "silo" of the application. We are slowly transitioning an older project from a mostly jQuery frontend so it's possible that as it evolves We'll be able to condense down to a single Vue instance but we're having no trouble with multiple instances. The ease of using Vue in existing projects was one of the biggest deciding factors in choosing it over another library such as react.

I have taken a different approach with green development, one instance and many components.

like image 41
Brendan Kendrick Avatar answered Sep 22 '22 09:09

Brendan Kendrick