This is a simple enough one, but I'm not getting the answer I need from the documentation. I'm in the process of learning Vue.js, and I don't really understand where Vue.extend fits in. I get Vue.component, but I don't see what Vue.extend does which Vue.component doesn't. Is it just a legacy feature from 1.0? If not, where is it useful in Vue 2.0?
Extending Components The pattern we will highlight today is the extends option (part of the Vue options API) that allows us to compose components and share functionality throughout different parts of your UI in a manageable, concise pattern.
VUE file extension is mainly associated with single file components used by Vue, an open-source, frontend JavaScript framework for building user interfaces. Such VUE single-file components are modules containing source code (typically for a single UI component) which can be exported or reused in a Vue application.
Components in Vue are composed of three parts; a template (which is like HTML), styles and JavaScript. These can be split into multiple files or the same .
Angular is an obvious choice for building enterprise-based applications because of its extensive built-in functionalities and community support. Vue is a technically sound framework when it comes to building user interfaces and solving complex problems.
Their guide in the previous Vuejs.org site had a document.
Copied from http://optimizely.github.io/vuejs.org/guide/composition.html which is forked from vuejs/Vuejs.org at version 0.10.6 of Vuejs.
It is important to understand the difference between
Vue.extend()
andVue.component()
. SinceVue
itself is a constructor,Vue.extend()
is a class inheritance method. Its task is to create a sub-class ofVue
and return the constructor.Vue.component()
, on the other hand, is an asset registration method similar toVue.directive()
andVue.filter()
. Its task is to associate a given constructor with a string ID so Vue.js can pick it up in templates. When directly passing in options toVue.component()
, it callsVue.extend()
under the hood.Vue.js supports two different API paradigms: the class-based, imperative, Backbone style API, and the markup-based, declarative, Web Components style API. If you are confused, think about how you can create an image element with
new Image()
, or with an<img>
tag. Each is useful in its own right and Vue.js tries to provide both for maximum flexibility.
In addition to the answers provided, there is a practical use case for calling Vue.extend()
directly if you are using TypeScript.
It is recommended in most cases to import component definitions from other files when they are needed, instead of registering them globally with Vue.component()
. It keeps things organized in larger projects and makes it easier to trace problems.
With the right library, TypeScript can look at your component definition and figure out what the type of the component will be when it's initialized, meaning it can then check the functions you've defined to see if they make sense (i.e. if you reference this.foo
there is actually a prop, data field, computed value, or method named foo
). If you use Vue.component()
it will be able to do this, but not if you use the other typical way of making components available, which is exporting object literals.
However, another option is to export your component definitions wrapped in Vue.extend()
. TypeScript will then be able to recognize it as a Vue component and run its type checking accordingly, but you won't have to abandon the best-practice pattern of exporting components rather than registering them globally.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With