【发布时间】:2018-09-03 18:11:21
【问题描述】:
我正在调用端点以带回一个对象,该对象确实获取数据,但速度不足以让组件获取数据并呈现。相反,组件在应该有数据的地方呈现空白值。
如果我在创建时中断代码,然后可能一秒钟后继续,文本会正确呈现。
如何实现它直到数据返回才呈现?
我的 API 调用:
checkScenarioType: function () {
this.$http.get('ScenariosVue/GetScenarioTypeFromParticipant/' + this.ParticipantId).then(response => {
// get body data
this.ScenarioType = response.body.value;
if (this.ScenarioType.timeConstraint) {
store.commit('switchConstraint');
}
}, response => {
// error callback
});
}
有问题的组件:
var questionArea = Vue.component('questionarea', {
props: ["scenariotype"],
data: function () {
return ({
position: "",
vehicleType: ""
});
},
methods: {
transformValuesForDisplay: function () {
switch (this.scenariotype.perspective) {
case 1: {
this.position = "Driver";
this.vehicleType = "Autonomous";
break;
}
case 2: {
this.position = "Passenger";
this.vehicleType = "Manually Driven";
break;
}
case 3: {
this.position = "Driver";
this.vehicleType = "Manually Driven";
break;
}
}
}
},
beforeMount() {
this.transformValuesForDisplay();
},
template:
`<h1>You are the {{ this.position }}! What should the {{ this.vehicleType }} car do?</h1>`
});
【问题讨论】:
标签: ajax web-services asynchronous vue.js vuejs2