我正在Vue中使用Surveyjs,并且试图从我的API中获得var json
个问题。
使用vue-resource,我向API发出GET请求,并将响应保存到questions
并将其放在var json
上
export default {
components: {
Survey
},
data(){
this.$http.get('api')
.then(res => res.json())
.then(questions => {
this.questions = questions;
});
var json = this.questions;
var model = new SurveyVue.Model(json);
return {
survey: model,
questions:''
}
}
}
使用console.log(json)
,我得到undefined
。那么,在这种情况下访问API响应的正确方法是什么?
答案 0 :(得分:1)
我认为您可以使用类似这样的东西:
export default {
components: {
Survey
},
data() {
return {
survey: {},
questions: ''
}
},
mounted() {
let self = this;
this.$http.get('api')
.then(questions => {
self.questions = questions;
});
this.survey = new SurveyVue.Model(this.questions);
}
}
您可以详细了解mounted
方法here。您现在应该可以访问您的调查数据。