Vuejs:动态递归组件

Ray*_*vie 1 javascript recursion components vue.js vuejs2

我正在尝试制作一个调用自身的自定义组件。我不断收到错误消息

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

我已经包含了一个名称选项,如下所示,但这并不能解决问题。

知道它可能是什么吗?

<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>
Run Code Online (Sandbox Code Playgroud)

Moh*_*_PH 5

当我取下components钥匙并用它的名字来称呼它时,它为我工作。这是代码沙箱中的运行示例,以下是供将来参考的代码:

<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>
Run Code Online (Sandbox Code Playgroud)