简体   繁体   English

组件内的组件 - VueJS

[英]Component inside Component - VueJS

I am having a hard time to understand this, so I have a component which is already complied which is a grid , now when I click on a button a modal pops-up and display another grid inside the modal at this point my code looks like this for the modal pop-up我很难理解这一点,所以我有一个已经编译好的组件,它是一个grid ,现在当我单击一个按钮时,会弹出一个模态并在模态内显示另一个网格,此时我的代码看起来像这用于模态弹出窗口

<template>

    <transition v-if="this.modalVisible" v-bind:title.sync="this.modalVisible" name="modal">
        <div class="modal-mask">
            <div class="modal-wrapper">
                <div class="modal-container">

                    <div class="modal-header">
                        {{ modalHeaderName }}
                    </div>

                    <div class="modal-body">
                     //this is another component
                    <grid-data :grid-values="dummy" :tool-bar="false"></grid-data> 
                    </div>

                    <div class="modal-footer">

                    </div>
                </div>
            </div>
        </div>
    </transition>

</template>

<script>
    import DataTable from './core/gridTable.vue';


    export default {
        components:{
            JqxButton,
            'grid-data' : DataTable,
        },
        props : {
            modalHeaderName : String,
            modalVisible : Boolean
        },
        data: function () {
            return {
                buttonWidth: 120,
                buttonHeight: '100%',
                value: this.buttonName,
                dummy : [
                    { name: 'ProductName', type: 'string' },
                    { name: 'QuantityPerUnit', type: 'int' },
                    { name: 'UnitPrice', type: 'float' },
                    { name: 'UnitsInStock', type: 'float' },
                    { name: 'Discontinued', type: 'bool' }
                ],
            }
        }
    }
</script>

Now, the grid is a vue component which was already complied and rendered, now will I import it again it says现在, grid是一个已经编译和渲染的 vue 组件,现在我将它再次导入它说

[Vue warn]: Failed to mount component: template or render function not defined. [Vue 警告]:无法挂载组件:未定义模板或渲染函数。

<template>
    <div>
        <!-- sync here is, getting the value from the updated modal-->
        <custom-modal :modal-visible="this.showModal"  v-bind:showModal.sync="showModal" :modal-header-name="this.modalHeaderName"></custom-modal>
        <JqxGrid :width="width" :source="dataAdapter" :columns="gridValues"
                 :pageable="true" :autoheight="true" :sortable="true"
                 :altrows="true" :enabletooltip="true" :editable="true"
                 :selectionmode="'multiplecellsadvanced'"  :showtoolbar="this.toolBar" :rendertoolbar="rendertoolbar">
        </JqxGrid>
    </div>

</template>
<script>
    import JqxGrid from "../jqx-vue/vue_jqxgrid.vue";
    import CustomModal from "../customModal";
    export default {
        components: {
            JqxGrid,
            'custom-modal' : CustomModal
        },
        // added the name here
        name: 'jqx-grid',
        props : {
            gridValues : Array,
            toolBar : Boolean
        },
        data: function () {
            return {
                showModal : false,
                modalHeaderName : '',
                width: '100%',
                dataAdapter: new jqx.dataAdapter({
                     datatype: 'xml',
                     datafields : this.gridValues,
                     url: ''
                }),
                columns: []
            }
        },
        mounted: function () {
            this.createButtons();
        },
        methods: {
            rendertoolbar: function (toolbar) {
                let buttonsContainer = document.createElement('div');
                buttonsContainer.style.cssText = 'overflow: hidden; position: relative; margin: 5px;';

                let addButtonContainer = document.createElement('div');
                let deleteButtonContainer = document.createElement('div');

                addButtonContainer.id = 'addButton';
                deleteButtonContainer.id = 'deleteButton';

                addButtonContainer.style.cssText = 'float: left; margin-left: 5px;padding-bottom:25px;';
                deleteButtonContainer.style.cssText = 'float: left; margin-left: 5px;padding-bottom:25px;';

                buttonsContainer.appendChild(addButtonContainer);
                buttonsContainer.appendChild(deleteButtonContainer);
                toolbar[0].appendChild(buttonsContainer);
            },
            createButtons: function () {

                let addButtonOptions = {
                    height: 25, value: '&nbsp; <i class="fa fa-plus" style="padding-top:3px"></i> &nbsp;Add Items &nbsp;',
                };
                let addButton = jqwidgets.createInstance('#addButton', 'jqxButton', addButtonOptions);
                let deleteButtonOptions = {
                    height: 25, value: '&nbsp; <i class="fa fa-ban" style="padding-top:3px"></i> &nbsp;Remove All &nbsp;',
                };
                let deleteButton = jqwidgets.createInstance('#deleteButton', 'jqxButton', deleteButtonOptions);

                // add new row.
                addButton.addEventHandler('click', (event) => {
                    this.showModal = true;
                    this.modalHeaderName = 'Bulk Add Items';
                });
                // delete selected row.
                deleteButton.addEventHandler('click', (event) => {
                   // alert('delete')
                });

            },
            cellsrenderer: function (row, columnsfield, value, defaulthtml, columnproperties, rowdata) {
                if (value < 20) {
                    return '<span style="margin: 4px; float: ' + columnproperties.cellsalign + '; color: #ff0000;">' + value + '</span>';
                }
                else {
                    return '<span style="margin: 4px; float: ' + columnproperties.cellsalign + '; color: #008000;">' + value + '</span>';
                }
            }
        }
    }
</script>

How can I overcome this issue?我该如何克服这个问题?

I have seen question like this which says the component grid is trying to compile again and hence the error but I am not sure of that, so we should be using the complied version of the grid component.我见过这样的问题,它说组件网格正在尝试再次编译,因此出现错误,但我不确定,所以我们应该使用网格组件的编译版本。

NOTE: Using Vue with Laravel 5.4注意:在 Laravel 5.4 中使用 Vue

Error Image错误图像在此处输入图像描述

I didn't see an obvious error when you first posted the code.当您第一次发布代码时,我没有看到明显的错误。 Right now I see JqxButton inside components of the upper code block, which would be undefined.现在我在上面代码块的components中看到JqxButton ,它是未定义的。 In your code, you always import some components for which we can't see the code.在您的代码中,您总是会导入一些我们看不到代码的组件。

Generally, when I'm in a situation like this and everything seems to be looking okay, I remove all sub-components and see if the error goes away.通常,当我处于这种情况并且一切看起来都还不错时,我会删除所有子组件并查看错误是否消失。 Then, I re-add one component after each other until I hit the error again and try to debug it there.然后,我一个接一个地重新添加一个组件,直到我再次遇到错误并尝试在那里调试它。

From your description, I suspect you have some kind of cycle in your dependencies and you might find the documentation about circular references helpful.根据您的描述,我怀疑您的依赖项中有某种循环,您可能会发现有关循环引用的文档很有帮助。

Vue needs a lazy import for circular dependencies: Vue 需要一个惰性导入来实现循环依赖:

components: {
  "my-circular-dependency": () => import("./my-circular-dependency.vue");
}

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

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