Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extend another VueJS component in a single-file component? (ES6 vue-loader)

I am using vue-loader to construct my *.vue single-file components, but I am having trouble with the process of extending a single-file component from another.

If one component follows the spec to export default { [component "Foo" definition] }, I would think it is just a matter of importing this component (as I would with any child component) and then export default Foo.extend({ [extended component definition] })

Unfortunately this does not work. Can anyone please offer advice?

like image 514
Rhys Avatar asked Mar 12 '16 22:03

Rhys


People also ask

Can you extend a Vue component?

In Vue. JS it's possible to extend existing components . In Vue GWT we can use Java inheritance to extend a base Component. This allows you to reuse some behavior across Components.

How do I import one component to another component in Vue?

You need to create your portfolio component first e.g. in src/components/Projects/Portfolio. vue and then import it inside the script tag within your Landing. vue component.

How do I transfer data from one Vue component to another?

Using Props To Share Data From Parent To Child. VueJS props are the simplest way to share data between components. Props are custom attributes that we can give to a component. Then, in our template, we can give those attributes values and — BAM — we're passing data from a parent to a child component!

How do you extend Vue?

You use Vue. extend to create component definition (called "component constructor" in old documentation) and Vue. component to register it so it can be used in template to actually create component instance. However, everywhere in Vue API where you can pass "component constructor" created by Vue.


1 Answers

After some testing, the simple solution was to be sure to export a Vue.extend() object rather than a plain object for any component being extended.

In my case, the base component:

import Vue from 'vue'  export default Vue.extend({ [component "Foo" definition] }) 

and the extended component:

import Foo from './Foo'  export default Foo.extend({ [extended component definition] }) 
like image 97
Rhys Avatar answered Sep 23 '22 06:09

Rhys