【发布时间】:2019-03-30 18:11:43
【问题描述】:
我有一个带有两个本地组件的 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>
如何访问本地组件函数中的 props 值?
【问题讨论】:
-
试试this.props?
-
我可以访问道具,问题是没有定义值。 axios 中的 formID 未定义。
-
你能在你使用
custom-erp-body的地方显示html模板代码吗? -
请检查一下,我已经更新了@ittus问题中的HTML
标签: javascript vue.js vue-component