首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Vue.js中的嵌套组件:未能装入组件:未定义的模板或呈现函数

Vue.js中的嵌套组件:未能装入组件:未定义的模板或呈现函数
EN

Stack Overflow用户
提问于 2017-03-06 19:59:32
回答 1查看 1.9K关注 0票数 3

我正在使用Vue-CLI并得到这个错误。它可以在<comment>组件中找到。

当CommentForm的submitComment()方法触发并将注释对象添加到要呈现的selfComments时,就会发生错误。可能是因为他们互相推荐信什么的,但我不确定。

我试图只包括相关的信息:

编辑:我认为这与 https://forum.vuejs.org/t/how-to-have-indirectly-self-nested-components-in-vue-js-2/1931有关

CommentForm.vue

代码语言:javascript
复制
<template>
   ...
        <ul class="self-comments">
            <li is="comment" v-for="comment in selfComments" :data="comment"></li>
        </ul>
   ...
</template>

<script>    
import Comment from 'components/Comment'

export default {
    name: 'comment-form',
    components: {
        Comment
    },
    props: ['topLevel', 'replyTo', 'parentId'],
    data() {
        return {
            text: '',
            postingStatus: 'Post',
            error: false,
            selfComments: []
        }
    },
    methods: {
        submitComment() {
            ...
        }
    }
}
</script>

<style scoped lang="scss">
...
</style>

Comment.vue

代码语言:javascript
复制
<template>
       ...
             <comment-form v-if="replyFormOpen" :top-level="false" :reply-to="data.username" :parent-id="data.id"></comment-form>

             <!-- recursive children... -->
             <ul>
                 <li is="comment" @delete="numComments -= 1" v-for="comment in data.children" :data="comment"></li>
             </ul>

       ...
</template>

** importing CommentForm here seems to cause the issue

<script>
import CommentForm from 'components/CommentForm'

export default {
    name: 'comment',
    components: {
        CommentForm
    },
    props: ['data'],
    data() {
        return {
            deleteStatus: 'Delete',
            deleted: false,
            error: false,
            replyFormOpen: false
        }
    },
    methods: {
        ...
    }
}
</script>

<style scoped lang="scss">
...
</style>

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-06 22:27:35

我想你遇到了这个问题:组件之间的循环引用

CommentForm组件中,尝试在beforeCreate()事件期间注册Comment组件。这将帮助Vue确定解析组件的正确顺序。

代码语言:javascript
复制
<script>
export default {
    name: 'comment-form',
    props: ['topLevel', 'replyTo', 'parentId'],
    data() {
        return {
            text: '',
            postingStatus: 'Post',
            error: false,
            selfComments: []
        }
    },
    methods: {
        submitComment() {
            ...
        }
    },
    beforeCreate() {
        // register the Comment component here!!!!
        this.$options.components.Comment = require('components/Comment.vue');
   }
}
</script>
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42634488

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档