简体   繁体   English

Vuejs:动态递归组件

[英]Vuejs: Dynamic Recursive components

I am trying to make a custom component that call itself. 我正在尝试创建一个自称的自定义组件。 i keep getting an error 我一直在收到错误

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

I have included a name option as you can see below but this doesn't solve the problem. 我已经包含了一个名称选项,如下所示,但这并没有解决问题。

Any idea what it could be? 知道它可能是什么?

<template>
  <div>
        <h4>I am a component that can call itself below</h4>

        <v-select :items="['TestRec', 'normal']" v-model="comp"></v-select>

        <hr />
        <component :is="comp"></component>
  </div>
</template>

<script>
import TestRec from "./TestRec";
export default {
    name: 'New-Test-Rec', //Name tried 'Test-Rec' too. Same result
    components: {
        TestRec
    },
    data(){
        return {
            comp: null
        }
    }
}
</script>

Worked for me when I removed components key and called it with its name. 当我删除components密钥并使用其名称调用它时,为我工作。 here is a running example in code sandbox and here is the code for future reference: 这是代码沙箱中的一个运行示例 ,这里是未来参考的代码:

<template>
    <div class="hello">
        <h4>I am a component that can call itself below</h4>
        <button @click="show = true">Load next</button>
    <hr />
    <component v-if="show" :is="'TestRec'"></component>
    </div>
</template>

<script>
    export default {
  name: 'TestRec',
  data() {
    return {
      msg: 'Welcome to Your Vue.js App',
      show: false
    }
  }
}

</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM