gpt4 book ai didi

javascript - Vue 组件自循环 : Failed to mount component: template or render function not defined

转载 作者:搜寻专家 更新时间:2023-10-30 22:24:55 24 4
gpt4 key购买 nike

似乎无法弄清楚这一点。我有一个主要的包装器组件,它使用另一个组件来呈现导航结构。

导航可以是多级深度的,因此需要动态生成。

包装看起来像这样:

<template>
<nav v-if="this.navigation.length">
<link-role :collection="navigation"></link-role>
</nav>
</template>
<script>
import LinkRole from './Formats/LinkRole';
export default {
components: {
'link-role': LinkRole,
},
props: {
navigation: {
type: Object,
default: () => { return {} }
}
}
}
</script>

LinkRole 组件如下:

<template>
<ul>
<li v-for="(item, index) in collection" :key="index">
<a v-if="item.url" :href="item.url" v-text="item.name"></a>
<span v-else v-text="item.name"></span>
<link-role v-if="item.items" :collection="item.items"></link-role>
</li>
</ul>
</template>
<script>
import LinkRole from './LinkRole';
export default {
components: {
'link-role': LinkRole,
},
props: {
collection: {
type: [Object, Array],
default: () => []
}
},
}
</script>

正如您在 LinkRole 中看到的那样,如果还有另一层 items.

通过这种方法我得到了

[Vue warn]: Failed to mount component: template or render function not defined.

但无法弄清楚是什么原因造成的。

最佳答案

根据docs你需要在你的组件中提供一个 name 选项,以便能够递归地使用它..

Components can recursively invoke themselves in their own template. However, they can only do so with the name option

import LinkRole from './LinkRole';
export default {
name: 'link-role',
components: {
'link-role': LinkRole,
},
props: {
collection: {
type: [Object, Array],
default: () => []
}
},
}

关于javascript - Vue 组件自循环 : Failed to mount component: template or render function not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52543628/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com