106

I'm a beginner with Vue.js and I'm trying to create an app that caters my daily tasks and I ran into Vue Components. So below is what I've tried but unfortunately, it gives me this error:

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

Any ideas, help, please?

new Vue({
  el : '#app',
  data : {
    tasks : [
      { name : "task 1", completed : false},
      { name : "task 2", completed : false},
      { name : "task 3", completed : true}
    ]
  },
  methods : {
  
  },
  computed : {
  
  },
  ready : function(){

  }

});

Vue.component('my-task',{
  template : '#task-template',
  data : function(){
    return this.tasks
  },
  props : ['task']
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.26/vue.js"></script>
<div id="app">
  <div v-for="task in tasks">
      <my-task :task="task"></my-task>
  </div>
  
</div>

<template id="task-template">
  <h1>My tasks</h1>
  <div class="">{{ task.name }}</div>
</template>

11 Answers 11

137

You forgot about the components section in your Vue initialization. So Vue actually doesn't know about your component.

Change it to:

var myTask = Vue.component('my-task', {
 template: '#task-template',
 data: function() {
  return this.tasks; //Notice: in components data should return an object. For example "return { someProp: 1 }"
 },
 props: ['task']
});

new Vue({
 el: '#app',
 data: {
  tasks: [{
    name: "task 1",
    completed: false
   },
   {
    name: "task 2",
    completed: false
   },
   {
    name: "task 3",
    completed: true
   }
  ]
 },
 components: {
  myTask: myTask
 },
 methods: {

 },
 computed: {

 },
 ready: function() {

 }

});

And here is jsBin, where all seems to works correctly: http://jsbin.com/lahawepube/edit?html,js,output

UPDATE

Sometimes you want your components to be globally visible to other components.

In this case you need to register your components in this way, before your Vue initialization or export (in case if you want to register component from the other component)

Vue.component('exampleComponent', require('./components/ExampleComponent.vue')); //component name should be in camel-case

After you can add your component inside your vue el:

<example-component></example-component>
Sign up to request clarification or add additional context in comments.

5 Comments

So If I have many components, I should then bind it on the components array?
@CodeDemon for sure. You can make it shorter if you will use ES6, like so components: {myTask}, Or if its some small component, you can create it inside components property. But it seems like not good way, because of app scale.
Any ideas why I have this Vue warning : vue.js:1023 [Vue warn]: data functions should return an object. (found in component: <my-task>)?
you need to write it like so in components: data: function() { return { property1: 1, property2: 2 } }
for my case, I had spelled components incorrectly as component. hence, I got this error.
65

Just define Vue.component() before new vue().

Vue.component('my-task',{
     .......
});

new Vue({
    .......
});

update

  • HTML converts <anyTag> to <anytag>
  • So don't use captital letters for component's name
  • Instead of <myTag> use <my-tag>

Github issue : https://github.com/vuejs/vue/issues/2308

1 Comment

This should be the main answer to the problem. I can confirm from the vuejs docs and by trying it that Vue.component should be placed before the root (or new Vue({})).
5

I just spared 1 hour with this message - usually easy to solve.

I got this error because I forgot to close my <script> tag in the parent component.

The browser console & my code editor gave no indication unfortunately.

2 Comments

mine is the dumbest, I put the component outside the #app element that's why it keep saying "component not found"
Thanks kind stranger,i spent like 3 hours on this and you saved my time.
4

Vue definitely has some bugs around this. I find that although registering a component like so

components: { MyComponent }

will work most of the time, and can be used as MyComponent or my-component automatically, sometimes you have to spell it out as such

components: { 'my-component' : MyComponent }

And use it strictly as my-component

Comments

4

In my case I had import {component} from "@/path/to/component.vue"

Changing it to import component from "@/path/to/component.vue" fixed the issue.

Comments

4

Don't overuse Vue.component(), it registers components globally. You can create file, name it MyTask.vue, export there Vue object https://v2.vuejs.org/v2/guide/single-file-components.html and then import in your main file, and don't forget to register it:

new Vue({
...
components: { myTask }
...
})

Comments

3

This solved it for me: I supplied a third argument being an object.

in app.js (working with laravel and webpack):

Vue.component('news-item', require('./components/NewsItem.vue'), {
    name: 'news-item'
});

Comments

3

OK, this error may seem obvious, but one day I was looking for an answer JUST TO FOUND OUT THAT I HAD 2 times COMPONENTS declared. it was driving me nuts as VueJS does not complain at all when you declare it 2 times, obvious I had a lot of code in between, and when I added a new component, I placed the declaration in the top, while I also had one close to the bottom. So next time looks for this first, saves a lot of time

1 Comment

I save kind of same problems. I use a image manager in OrchardCore.Midia, and on the same page, I include vue using <script src="..."></script. Vue not work until I remove vue from the image manager. One day, I include BootstrapVue in the same page, it not work and report Unknown custom element: <b-alert> until I remove vuedraggable from the image manager.
2

Be sure that you have added the component to the components.

For example:

export default {
data() {
    return {}
},
components: {
    'lead-status-modal': LeadStatusModal,
},
}

Comments

0

This is a good way to create a component in vue.

let template = `<ul>
  <li>Your data here</li>
</ul>`;

Vue.component('my-task', {
  template: template,
  data() {

  },
  props: {
    task: {
      type: String
    }
  },
  methods: {

  },
  computed: {

  },
  ready() {

  }
});

new Vue({
 el : '#app'
});

Comments

0

I was following along the Vue documentation at https://v2.vuejs.org/v2/guide/index.html when I ran into this issue.

Later they clarify the syntax:

So far, we’ve only registered components globally, using Vue.component:

   Vue.component('my-component-name', {
       // ... options ...
   })

Globally registered components can be used in the template of any root Vue instance (new Vue) created afterwards – and even inside all >subcomponents of that Vue instance’s component tree.

(https://v2.vuejs.org/v2/guide/components.html#Organizing-Components)

So as Umesh Kadam says above, just make sure the global component definition comes before the var app = new Vue({}) instantiation.

Comments

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.