Reputation: 4013
I have a Vue
Instance with two local components, both the components are having props
from the data of the Vue Instance. But when I try to access the props
values from one of the local component the values ae undefined.
This is the code
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);
})
}
}
}
},
})
This is the HTML usage of the component
<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>
How can I access the props values in the local component function?
Upvotes: 4
Views: 1465
Reputation: 7177
It seems that the problem is your prop names because Vue expects a kebab-cased form of your prop names in DOM templates.
HTML attribute names are case-insensitive, so browsers will interpret any uppercase characters as lowercase. That means when you’re using in-DOM templates, camelCased prop names need to use their kebab-cased (hyphen-delimited) equivalents.
So for currentModuleFormID
it expects current-module-form-i-d
in the DOM template, not current-module-form-id
as you would expect. Try changing currentModuleFormID
to currentModuleFormId
with a lower case d
at the end and use current-module-form-id
in the template and I'm guessing it should work.
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>
Upvotes: 1
Reputation: 22403
You have to pass props to custom-erp-body
component:
<custom-erp-body
:currentModuleFormID="currentModuleFormID"
:currentModuleReportID ="currentModuleReportID">
</custom-erp-body>
Upvotes: 0