Expanding Vue Functionality
If you're looking to enhance your current Vue component with a custom method, you can achieve this using the Vue.extend
function. Vue simplifies the process of cloning/extending components by utilizing the extends
option:
Option/ Composition - extends
import SourceComponent from 'node_modules/somePackageName/components/footer.vue'
export default {
name: "override",
extends: SourceComponent,
methods: {
thatActualMethodBehaviour(){}
}
}
Vue.extend
// This can be either a Function or an Object
import SourceComponent from 'node_modules/somePackageName/components/footer.vue'
if(typeof SourceComponent === 'function'){
export default SourceComponent.extend({
methods: {thatActualMethodBehaviour(){}}
})
} else {
export default Vue.extend(SourceComponent).extend({
methods: {thatActualMethodBehaviour(){}}
})
}
Option/ Composition - mixins
// Mixins must be an Object
import SourceComponentOption from 'node_modules/somePackageName/components/footer.vue'
if(typeof SourceComponent !== 'function'){
export default Vue.extend({
mixins: [SourceComponentOption],
methods: {thatActualMethodBehaviour(){}}
})
}
The Path to Expansion
import SourceComponentOption from 'node_modules/somePackageName/components/footer.vue'
export default Object.assign({}, SourceComponentOption, {methods: Object.assign({}, SourceComponentOption.methods, {thatActualMethodBehaviour: function(){}}))
TypeScript Integration: vue-property-decorator
import {Vue, Component} from 'vue-property-decorator'
import SourceComponent from 'node_modules/somePackageName/components/footer.vue'
@Component<CustomComponent>({...hooks})
export default class CustomComponent extends SourceComponent{
methods: thatActualMethodBehaviour(){}
}