2

Please tell me what are the differences between Vue.component and new Vue.

  1. The second parameter of the Vue.component method ( the options object ), is the same first parameter of new Vue constructor. right?

  2. Are there any differences between two methods, in meaning and parameter options?

Thanks

2 Answers 2

2

Let's introduce both of them first and we'll talk about the difference after it.

What is New Vue?

using new Vue we create a new vue instance; Every Vue application starts by creating a new Vue instance with the Vue function like following:

var vm = new Vue({
  // you'r options here
})

According to official docs(this link):

A Vue application consists of a root Vue instance created with new Vue, optionally organized into a tree of nested, reusable components.

now that we know what vue instance is let's take a look at components.

What is Vue.component?

Components are reusable Vue instances with a name and we can use them as custom elements inside a root Vue instance

What are the differences?

  1. Since components are reusable Vue instances, they accept the same options as new Vue, such as data, computed, watch, methods, and lifecycle hooks. The only exceptions are a few root-specific options like el.
  2. A root Vue instance is a Vue application launcher, Vue component is an extension of the Vue instance.

Read this post for additional info.

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

1 Comment

@itbestdevelopment Yes, the same except for root-specific options like el.
1

When you call new Vue(), you're creating what is called a vue instance. A Vue instance basically is a JavaScript object that has some capabilities to interact with the DOM.

Vue components happen to be Vue instances, too. But the main difference is that components belong to other components/vue instances, while bare instances are mounted directly into an existing element of the DOM (with existing, I mean HTML that is not generated by Vue and existed previously to the instance creation).

So, basically, the main difference comes from the use: you usually create a bare Vue instance with new Vue when you need to attach that instance directly to the DOM as your root element. Vue components, on the other hand, are always attached to ** other components**.

In general, it is really common to have a single root vue instance in your Vue application, but you could have several bare instances in the same web page: you could get creative and span several Vue instances and make them interact with the DOM at the same time, as if they were components.

However, it is better to have a single root element with several components, since it is easier to reason about your code in that way, and also facilitates the use of plugins like Vuex or Vue-router.

Maybe this link about this matter can serve you as well.

3 Comments

The second parameter of the Vue.component method ( the options object ), is the same as first parameter of new Vue constructor. right?
yes, they are pretty much the same but I don't think components use the el field, since their mounting spot is managed by the parent. Also, in components, data must be a function.
Thanks for the answer. Therefore, in new Vue constructor data option is an object or a function, instead in Vue.component method the data option is a function.right?

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.