0

我在 vue 中使用surveyjs,并试图var json从我的 API 中获取我的问题。

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

4

1 回答 1

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方法的更多信息。您现在应该能够访问您的调查数据。

于 2019-04-03T13:25:21.800 回答