我有一个包含两个本地组件的Vue实例,这两个组件都具有来自Vue实例数据的props。但是,当我尝试从一个本地组件访问props值时,值是未定义的。
这是密码
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用法
<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>如何访问本地组件函数中的道具值?

发布于 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,我猜它应该能工作。
var custom_erp_widget = new Vue({
el : '#custom-erp-widgets',
data : {
showContainerHeader : false,
currentModuleName : 'foo',
currentModuleFormId : '5',
currentModuleReportId : '6'
},
....<custom-erp-body
:current-module-form-id="currentModuleFormId"
:current-module-report-id ="currentModuleReportId">
</custom-erp-body>发布于 2018-10-26 05:22:38
您必须将道具传递给custom-erp-body组件:
<custom-erp-body
:currentModuleFormID="currentModuleFormID"
:currentModuleReportID ="currentModuleReportID">
</custom-erp-body>https://stackoverflow.com/questions/53000999
复制相似问题