从数据库获取Survey.js问题

时间:2019-04-03 13:16:19

标签: vue.js surveyjs

我正在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响应的正确方法是什么?

1 个答案:

答案 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。您现在应该可以访问您的调查数据。