did you register the component correctly? For recu

2020-02-10 11:33发布

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?

标签: vue.js
8条回答
祖国的老花朵
2楼-- · 2020-02-10 11:37

For those looking for an answer and the others haven't worked, this might:

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.TreeFolderContents = require('./tree-folder-contents.vue').default
}

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

components: {
  TreeFolderContents: () => import('./tree-folder-contents.vue')
}

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.

查看更多
地球回转人心会变
3楼-- · 2020-02-10 11:46

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>
查看更多
萌系小妹纸
4楼-- · 2020-02-10 11:47

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'],
    }

查看更多
霸刀☆藐视天下
5楼-- · 2020-02-10 11:52

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.

查看更多
\"骚年 ilove
6楼-- · 2020-02-10 11:56

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.

查看更多
Fickle 薄情
7楼-- · 2020-02-10 11:57

Make sure that the following are taken care of:

  1. Your import statement & it's path,

  2. Your component TagName you gave in the components {....} block,

  3. Your block name should be components in lower case.

Hope it will solve the issue.

查看更多
登录 后发表回答