Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend vue.js component from third-party library

I'm using component ElDatepicker from element-ui and I want to change it's template and event handler method. I'm trying to do something like this in single file component:

import Vue from 'vue';
import ElDatePicker from 'element-datepicker'
Vue.use(ElDatePicker)
var dpkr = Vue.component('ElDatePicker')
console.log(dpkr)
export default {
    extends: ['ElDatePicker']
}

But it doesn't work. How i can change it?

https://github.com/ElemeFE/element/tree/dev/packages/date-picker - component package

like image 533
Dan Avatar asked Feb 05 '17 06:02

Dan


People also ask

Can you extend Vue components?

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.

How do I force update my Vue component?

The best way to force Vue to re-render a component is to set a :key on the component. When you need the component to be re-rendered, you just change the value of the key and Vue will re-render the component.

How do I add external js scripts to VueJS components?

To add external JavaScript scripts to Vue. js components, we can add a script element with document. createElement . export default { //... mounted() { const recaptchaScript = document.


1 Answers

Your main problem is that extends should specify a single component and not an array. You should reference the component and not the name.

import Vue from 'vue';
import ElDatePicker from 'element-datepicker'

export default {
  extends: ElDatePicker
}

The repo you posted is from element-ui

Do npm install element-ui

Then:

import { DatePicker } from 'element-ui'
export default {
  // Use mixins for array syntax
  mixins: [DatePicker]
  // OR use extends without array
  extends: DatePicker
}
like image 122
Michael2 Avatar answered Sep 28 '22 16:09

Michael2