道具未传递到动态组件

时间:2018-11-22 02:23:22

标签: vue.js vuejs2 vue-dynamic-components

我正在渲染一个动态组件:

<component :is="element.name == 'text' ? element.component : false" v-bind="elementProps"></component>

具有:

computed: {
    element() {
        return {
            name: this.elementObject.type,
            component: {
                components: { TextInput },
                template: `<text-input :form-id="formId"
                                        :section-id="sectionId"
                                        :element-id="elementId"
                                        id="test2"
                            ></text-input>`
            },
        }
    },
    elementProps() {
        const props = {
            formId: this.formId,
            sectionId: this.sectionId,
            elementId: this.elementId,
            id: this.generateId()
        };
        return props;
    },
}

..但是尽管我传递了道具,但我得到了一个错误Property or method "formId" is not defined on the instance but referenced during render.。我在做什么错了?

1 个答案:

答案 0 :(得分:1)

在元素函数中创建组件时,您忘记了定义道具,请尝试:

component: {
  components: { TextInput },
  template: `<text-input :form-id="formId"
                         :section-id="sectionId"
                         :element-id="elementId"
                         id="test2"></text-input>`,
  props: ['formId', 'sectionId', 'elementId', 'id']
},
模板中的

formIdsectionIdelementId必须在组件中的某个位置定义为 props data 计算属性

相关问题