0

I have the following component defined as below:

import Vue from 'vue';
import Datepicker from 'vuejs-datepicker';

Vue.component('DatePicker', {
    template: `
    <datepicker name="date"></datepicker>
  `,
});

Which I am trying to import it from another file and use it in another component but for some reason I am getting error like:

[Vue warn]: Unknown custom element: - did you register the component correctly? For recursive components, make sure to provide the "name" option.

I am pretty new to Vue.js so I am wondering if someone can guide me what am I doing wrong here!

Here is how I am trying to use the component:

require('./datetime-picker');
<DatePicker></DatePicker>
1
  • Did you write Vue.component('DatePicker' .... before initialize Vue instance new Vue({ ... ? Commented Feb 27, 2019 at 13:02

2 Answers 2

1

To register a component globally, try this:

import Vue from 'vue';
import Datepicker from 'vuejs-datepicker';

Vue.component('DatePicker', Datepicker);

Now you will be able use it by importing it into other components, without needing to use components: { Datepicker }

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

Comments

0

If you are using any components inside a Vue.js template, you should declare that in components property of the component whoever is using it.

Make sure you add that as dependency in your component -

import Vue from 'vue';
import Datepicker from 'vuejs-datepicker';

Vue.component('DatePicker', {
    components: { Datepicker },
    template: `
    <datepicker name="date"></datepicker>
  `,
});

1 Comment

Most folks using Vue will use modern syntax: components: { Datepicker },

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.