首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >访问本地组件vuejs中的道具值

访问本地组件vuejs中的道具值
EN

Stack Overflow用户
提问于 2018-10-26 03:30:44
回答 2查看 1.3K关注 0票数 4

我有一个包含两个本地组件的Vue实例,这两个组件都具有来自Vue实例数据的props。但是,当我尝试从一个本地组件访问props值时,值是未定义的。

这是密码

代码语言:javascript
复制
var custom_erp_widget = new Vue({
    el : '#custom-erp-widgets',
    data : {
        showContainerHeader : false,
        currentModuleName : 'foo',
        currentModuleFormID : '5',
        currentModuleReportID : '6'
    },
    components : {
        'custom-erp-header' : {
            template : '<div class="col-12" id="custom-erp-widget-header">'+
                        '{{ currentModuleName.toUpperCase() }}'+
                       '</div>',
            props : ['currentModuleName']
        },
        'custom-erp-body' : {
            template : '<div class="col-12" id="custom-erp-widget-body">'+
                       '</div>',
            props : ['currentModuleFormID','currentModuleReportID'],
            // for emitting events so that the child components
            // like (header/body) can catch and act accordingly
            created() {
                var _this = this;
                eventHub.$on('getFormData', function(e) {
                    if(e == 'report'){
                        console.log(_this.$props);

                        _this.getReportData();
                    }
                    else if(e == 'form'){
                        console.log(_this.$props);
                        _this.getFormData();
                    }

                });

              },

            methods : {
                // function to get the form data from the server
                // for the requested form
                getFormData : function(){
                    var _this = this;
                    //here the logs are returinig undefined
                    //but it is having values in the data from the root Instance
                    console.log(_this.$props.currentModuleFormID);
                    console.log(_this.currentModuleFormID);

                    axios
                        .get('http://localhost:3000/getFormData',{
                            params: {
                                formID: _this.currentModuleFormID + 'a'
                            }
                        })
                        .then(function(response){

                            console.log(response);

                        })
                }

            }

        }
    },

})

这是组件的HTML用法

代码语言:javascript
复制
<div class="row" id="custom-erp-widgets" v-show="showContainerHeader">

    <custom-erp-header :current-module-name='currentModuleName'></custom-erp-header>    
    <custom-erp-body></custom-erp-body>

</div>

如何访问本地组件函数中的道具值?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-10-26 05:20:43

问题似乎在于您的道具名称,因为Vue希望DOM模板中的道具名称有一个以烤肉串大小写的形式。

HTML属性名不区分大小写,因此浏览器会将任何大写字符解释为小写字符.这意味着当您使用-DOM模板时,camelCased道具名称需要使用它们的kebab大小写(连字符分隔)等价物。

因此,对于currentModuleFormID,它期望DOM模板中的current-module-form-i-d,而不是您所期望的current-module-form-id。尝试将currentModuleFormID改为currentModuleFormId,并在模板中使用小写d,并在模板中使用current-module-form-id,我猜它应该能工作。

代码语言:javascript
复制
var custom_erp_widget = new Vue({
    el : '#custom-erp-widgets',
    data : {
        showContainerHeader : false,
        currentModuleName : 'foo',
        currentModuleFormId : '5',
        currentModuleReportId : '6'
    },
  ....
代码语言:javascript
复制
<custom-erp-body 
  :current-module-form-id="currentModuleFormId" 
  :current-module-report-id ="currentModuleReportId">
</custom-erp-body>
票数 1
EN

Stack Overflow用户

发布于 2018-10-26 05:22:38

您必须将道具传递给custom-erp-body组件:

代码语言:javascript
复制
<custom-erp-body 
  :currentModuleFormID="currentModuleFormID" 
  :currentModuleReportID ="currentModuleReportID">
</custom-erp-body>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53000999

复制
相关文章

相似问题

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