search

Home  >  Q&A  >  body text

javascript - vue uses asynchronous components combined with is to load components on demand.

<el-tabs v-model="activeName" @tab-click="handleClick" type="border-card">
      <el-tab-pane v-for="item in menu" :label="item.name" :name="item.name" :key="item" :component="item.component"><component :is="item.component"></component></el-tab-pane>
    </el-tabs>
methods: {
    handleClick (tab, event) {
        // 异步加载组件
        let getCompoentIndex = this.menu.findIndex(x => x.name === tab.name)
        let component = this.menu[getCompoentIndex].component
        if (!this.menu[getCompoentIndex].loading) {
          this.menu[getCompoentIndex].loading = true
          Vue.component(component, function (resolve, reject) {
            setTimeout(function () {
              require([`./${component}.vue`], resolve)//比如 abc.vue
            }, 1000)
          })
        }
      }
  }

When clicked, the asynchronous component is loaded (the component can be loaded), but the following error is reported

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

Trying to add a name to the abc component still reports this error, does anyone know how to solve it?
abc.vue

export default {
    name: 'abc',
}
仅有的幸福仅有的幸福2742 days ago1297

reply all(2)I'll reply

  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-06-26 10:57:50

    The method I found is to add if judgment

    <el-tab-pane v-for="item in menu" :label="item.name" :name="item.name" :key="item" :component="item.component"><component :is="item.component" v-if='flag'></component></el-tab-pane>
    data:()=>({
        flag: false
    })

    Then set the flag to true when clicking to solve the error problem

    reply
    0
  • 漂亮男人

    漂亮男人2017-06-26 10:57:50

    I solved it using WEBPACK.
    You can check out my projects.

    reply
    0
  • Cancelreply