110

I configured 'i-tab-pane': Tabpane but report error,the code is bellow:

<template>
  <div class="page-common">
    <i-tabs>
      <i-tab-pane label="wx">
        content
      </i-tab-pane>
    </i-tabs>
  </div>
</template>

<script>

  import {
    Tabs,
    Tabpane
  } from 'iview'

  export default{
    name:"data-center",
    data(){
      return {msg: 'hello vue'}
    },
    components: {
      'i-tabs' : Tabs,
      'i-tab-pane': Tabpane
    }
  }
</script>

Error traceback:

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

found in

---> <DataCenter> at src/views/dc/data-center.vue
       <Index> at src/views/index.vue
         <App> at src/app.vue

I have tried in the main.js to global configuration:

Vue.component("Tabpane", Tabpane);

but still do not work. How to resolve this issue?

user7693832
  • 6,119
  • 19
  • 63
  • 114

27 Answers27

113

If you're using a component within a component (e.g. something like this in the Vue DOM):

App
  MyComponent
   ADifferentComponent
     MyComponent

Here the issue is that MyComponent is both the parent and child of itself. This throws Vue into a loop, with each component depending on the other.

There's a few solutions to this:

 1. Globally register MyComponent

vue.component("MyComponent", MyComponent)

2. Using beforeCreate

beforeCreate: function () {
  this.$options.components.MyComponent = require('./MyComponent.vue').default
}

3. Move the import into a lambda function within the components object

components: {
  MyComponent: () => import('./MyComponent.vue')
}

My preference is the third option, it's the simplest tweak and fixes the issue in my case.


More info: Vue.js Official Docs — Handling Edge Cases: Circular References Between Components

Note: if you choose method's 2 or 3, in my instance I had to use this method in both the parent and child components to stop this issue arising.

tony19
  • 125,647
  • 18
  • 229
  • 307
fredrivett
  • 5,419
  • 3
  • 35
  • 48
  • 1
    Yes fredrivett your solution works for me. Thank you! – Suhas Bhosale Jan 02 '20 at 09:31
  • Happy to help @SuhasBhosale – fredrivett Jan 02 '20 at 12:08
  • 2
    The dynamic import works like a charm, when you want to do recursive components. – Kasper Seweryn Feb 22 '20 at 15:33
  • Excellent. Only place online with the right answer, I wonder why the docs don't explain it better. – Dirigible Apr 28 '20 at 12:59
  • For methods 2 and 3, is there a way to set the name of the component as you can in method 1? For example, if you wanted component TreeFolderContents to be "tree-foldercontents" rather than "tree-folder-contents". Or is this taken from the "name" prop in the component? – Zakalwe Jul 06 '20 at 11:08
  • @Zakalwe Option 1 registers `MyComponent` globally, to be used as ``. Option's 2 & 3 register these as `TreeFolderContents` and used as ``. The name all three of these is set in the component itself (`export default { name: "your-name-here", ... }`) which I believe is used for the vue js dev tools and enriching error messages, but is not used for implementing the component. So all three options allow for importing with a different JS name to be used in the Vue DOM, or changing the component name used for the dev tools and error messages. – fredrivett Jul 06 '20 at 17:02
  • how about if i directly make component on template ? for example without any import or class that define yogi-arif component ??? – Yogi Arif Widodo Aug 20 '22 at 22:15
  • @YogiArifWidodo I'm not sure what you mean here, do you mean having two components in one file? – fredrivett Aug 22 '22 at 10:05
  • the other file is work but sometimes i got the issue but now solved by directly use tips on number 3 only – Yogi Arif Widodo Mar 13 '23 at 10:07
66

Since you have applied different name for the components:

components: {
      'i-tabs' : Tabs,
      'i-tab-pane': Tabpane
    }

You also need to have same name while you export: (Check to name in your Tabpane component)

name: 'Tabpane'

From the error, what I can say is you have not defined the name in your component Tabpane. Make sure to verify the name and it should work fine with no error.

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
55

Wasted almost one hour, didn't find a solution, so I wanted to contribute =)

In my case, I was importing WRONGLY the component.. like below:

import { MyComponent } from './components/MyComponent'

But the CORRECT is (without curly braces):

import MyComponent from './components/MyComponent'
Marco Arruda
  • 699
  • 6
  • 17
  • import { } from "module"; why this statement is not correct? – Ali May 04 '20 at 22:32
  • I also spent WAY too long on this... I think it was importing as an object because I used some syntax shortcuts in vs code. – austriker May 12 '20 at 17:21
  • 1
    This mistake was simple but I didn't had not noticed, kudos ! – Manuel Alanis Sep 10 '20 at 17:48
  • 2
    @Ali, if you use brackets `{}`, you are trying to destructure an imported object. See this example: https://stackoverflow.com/questions/33524696/es6-destructuring-and-module-imports. In Vue.js modules, you want to import the whole object, not a single attribute of it – Marco Arruda Sep 15 '20 at 18:13
15

One of the mistakes is setting components as array instead of object!

This is wrong:

<script>
import ChildComponent from './ChildComponent.vue';
export default {
  name: 'ParentComponent',
  components: [
    ChildComponent
  ],
  props: {
    ...
  }
};
</script>

This is correct:

<script>
import ChildComponent from './ChildComponent.vue';
export default {
  name: 'ParentComponent',
  components: {
    ChildComponent
  },
  props: {
    ...
  }
};
</script>

Note: for components that use other ("child") components, you must also specify a components field!

Alex
  • 8,093
  • 6
  • 49
  • 79
AMTourky
  • 1,190
  • 13
  • 25
12

For recursive components that are not registered globally, it is essential to use not 'any name', but the EXACTLY same name as your component.

Let me give an example:

<template>
    <li>{{tag.name}}
        <ul v-if="tag.sub_tags && tag.sub_tags.length">
            <app-tag v-for="subTag in tag.sub_tags" v-bind:tag="subTag" v-bind:key="subTag.name"></app-tag>
        </ul>
    </li>
</template>

<script>
    export default {
        name: "app-tag",  // using EXACTLY this name is essential

        components: {

        },

        props: ['tag'],
    }

Lambert
  • 310
  • 3
  • 8
6

I had this error as well. I triple checked that names were correct.

However I got this error simply because I was not terminating the script tag.

<template>
  <div>
    <p>My Form</p>
    <PageA></PageA>        
  </div>
</template>

<script>
import PageA from "./PageA.vue"

export default {
  name: "MyForm",
  components: {
    PageA
  }
}

Notice there is no </script> at the end.

So be sure to double check this.

tno2007
  • 1,993
  • 25
  • 16
3

If you have path to the component (which causes a cycle) to index.js, cycle will be begin. If you set path directly to component, cycle will be not. For example:

// WRONG:
import { BaseTable } from @/components/Base'; // link to index.js

// SUCCESS:
import BaseTable from @/components/Base/Table.vue';
daewoo
  • 39
  • 2
1

I had this error and discovered the issue was because the name of the component was identical to the name of a prop.

import Control from '@/Control.vue';
export default {
    name: 'Question',
    components: {
        Control
    },
    props: ['Control', 'source'],

I was using file components. I changed the Control.vue to InputControl.vue and this warning disappeared.

Xeg
  • 11
  • 1
1

The high votes answer is right. You can checkout that you have applied different name for the components. But if the question is still not resolved, you can make sure that you have register the component only once.

components: {
    IMContainer,
    RightPanel
},
methods: {},
components: {
    IMContainer,
    RightPanel
}
  

we always forget that we have register the component before

1

This is very common error that we face while starting any Project Vue. I spent lot of time to search this error and finally found a Solution. Suppose i have component that is "table.vue",

i.e components/table.vue

In app.js

Vue.component('mytablecomp', require('./components/table.vue').default);

So in in your index.blade file call component as

<mytablecomp></mytablecomp>

Just you need to keep in mind that your component name is in small not in large or camel case. Then my above code will surely work for you.

Thanks

1

We've struggled with this error twice now in our project with different components. Adding name: "MyComponent" (as instructed by the error message) to our imported component did not help. We were pretty sure our casing was correct, as we used what is in the documentation, which worked fine for the other 99% of our components.

This is what finally worked for us, just for those two problematic components:

Instead of this (which, again, works for most of our components):

import MyComponent from '@/components/MyComponent';
export default {
  components: {
    MyComponent
}

We changed it to ONLY this:

export default {
  components: {
    MyComponent: () => import('@/components/MyComponent')
}

I can't find the documentation we originally found for this solution, so if anyone has any references, feel free to comment.

Lys777
  • 426
  • 1
  • 3
  • 11
  • This worked for me, pretty easy solution, not sure why this is not up voted more. Thanks @Lys777 – Janpan Nov 07 '22 at 11:57
1

If you are using Vue Class Component, to register a component "ComponentToRegister" you can do

import Vue from 'vue'
import Component from 'vue-class-component'
import ComponentToRegister from '@/components/ComponentToRegister.vue'

@Component({
  components: {
    ComponentToRegister
  }
})
export default class HelloWorld extends Vue {}
0

Make sure that the following are taken care of:

  1. Your import statement & its path

  2. The tag name of your component you specified in the components {....} block

Alex
  • 8,093
  • 6
  • 49
  • 79
Arslan
  • 67
  • 1
  • 5
0

Adding my scenario. Just in case someone has similar problem and not able to identify ACTUAL issue.

I was using vue splitpanes.

Previously it required only "Splitpanes", in latest version, they made another "Pane" component (as children of splitpanes).

Now thing is, if you don't register "Pane" component in latest version of splitpanes, it was showing error for "Splitpanes". as below.

[Vue warn]: Unknown custom element: <splitpanes> - did you register the component correctly? For recursive components, make sure to provide the "name" option.
Anonymous Creator
  • 2,968
  • 7
  • 31
  • 77
0

In my case it was the order of importing in index.js

/* /components/index.js */
import List from './list.vue';
import ListItem from './list-item.vue';

export {List, ListItem}

and if you use ListItem component inside of List component it will show this error as it is not correctly imported. Make sure that all dependency components are imported first in order.

Hassan Juniedi
  • 403
  • 5
  • 14
0

This is WRONG:

import {
    Tabs,
    Tabpane
  } from 'iview'

This is CORRECT:

import Iview from "iview";
const { Tabs, Tabpane} = Iview;
David Buck
  • 3,752
  • 35
  • 31
  • 35
0

In my case (quasar and command quasar dev for testing), I just forgot to restart dev Quasar command.

It seemed to me that components was automatically loaded when any change was done. But in this case, I reused component in another page and I got this message.

Plaute
  • 4,669
  • 2
  • 17
  • 19
0

i ran into this problem and below is a different solution. I were export my components as

export default {
    MyComponent1,
    MyComponent2
}

and I imported like this:

import { MyComponent1, MyComponent2} from '@/index'

export default {
  name: 'App',
  components: {
    MyComponent1,
    MyComponent2
  },
};

And it gave this error.

The solution is:

Just use export { ... } don't use export default

doğukan
  • 23,073
  • 13
  • 57
  • 69
0

In my case, i was calling twice the import...

@click="$router.push({ path: 'searcherresult' })"

import SearcherResult from "../views/SearcherResult"; --- ERROR

Cause i call in other component...

rod
  • 11
  • 1
0

The error usually arises when we have used the Component (lets say VText) but it has not been registered in the components declaration of the Parent Component(lets say Component B).

The error is more likely to occur when using components in a recursive manner. For example using tag=VText in an tag, as importing the component in a such case will result in error from Eslint as the component is not directly being used in the template. While not importing the component will cause an error in the console saying the component has not been registered.

In this case, it is a better approach to suppress the ESLinter on registration line of the Component(VText in this case). This suppression is done through writing // eslint-disable-next-line vue/no-unused-components

Example code is below

    <template>
        <i18n path="AssetDict.Companies" tag="VText">
          <template>
            <span class="bold-500">Hi This is a text</span>
          </template>
        </i18n>
    </template>

    <script>

    import { VButton, VIcon, VTooltip, VText } from 'ui/atoms'
    
    export default {
      name: 'ComponentB',
      components: {
        VButton,
        VIcon,
        CompaniesModifyColumn,
        VTooltip,
        // eslint-disable-next-line vue/no-unused-components
        VText,
      },
}

</script>
0

I just encountered this. Easy solution when you know what to look for.

The child component was the default export in it's file, and I was importing using:

import { child } from './filename.vue'

instead of

import child from './filename.vue'.
BlakeyUK
  • 83
  • 1
  • 9
0

What happened to me was I had correctly registered the component in components but I had another components key defined at the bottom of my component, so I had two components definitions and it looked like the latter one overrode the previous one. Removing it made it work.

technophyle
  • 7,972
  • 6
  • 29
  • 50
0

I encounter same error msg while using webpack to async load vue component.

function loadVMap() {
  return import(/* webpackChunkName: "v-map" */ './components/map.vue')
  .then(({ default: C }) => {

      Vue.component('ol-map',C);

     return C;
    })
    .catch((error) => 'An error occurred while loading the map.vue: '+error);

}

I found that the then function never executed.

so I reg this component out of webpack import

import Map from './components/map.vue' 
Vue.component('ol-map',Map);

Then I could gain the detailed error msg which said I used a var which is not imported yet.

scil
  • 169
  • 10
0

The question has been answered very well by @fredrivett here, but I wanted to add some context for other encountering the Circular Reference error when dealing with variables in general.

This error happens with any exported object not just components.

Exporting a variable from parent and importing it in a nested child:

EXAMPLE

<script>
// parent

export const FOO = 'foo';
...
</script>

❌ WRONG

<script>
// child

import { FOO } from 'path/to/parent'

export default {
  data() {
    return {
      FOO
    }
  }
}
</script>

✅ CORRECT

<script>
// child

export default {
  data() {
    return {
      FOO: require('path/to/parent').FOO
    }
  }
}
</script>

Note: in case you are dealing with objects you might want to create a global state which might serve you better.

I'm curious to know if this approach makes sense or it's an anti pattern.

tony19
  • 125,647
  • 18
  • 229
  • 307
a.barbieri
  • 2,398
  • 3
  • 30
  • 58
0

I ran into this problem when:

  1. I had components defined twice.
  2. Used component instead of components.

I hope this helps others.

dstran
  • 47
  • 7
0

In my case the child component name was "ABCChildComponent" and I was referring in the HTML as assuming it to work correctly. But, the correct name should be or . Hence, changed the name to "AbcChildComponent" and referring in the HTML works fine.

-1

WRONG WAY :

import completeProfile from "@/components/modals/CompleteProfile";

export default {
  components: completeProfile
};

RIGHT WAY :

import completeProfile from "@/components/modals/CompleteProfile";

export default {
  components: {completeProfile} // You need to put the component in brackets
};
Parth Developer
  • 1,371
  • 1
  • 13
  • 30