Axios Response数据和Vue JS,使用数据

时间:2017-10-11 08:06:56

标签: javascript vue.js vuejs2 axios

Vue的新手而不是Javascript的爱好者所以我有一个Vue组件,它使用Axios来获取控制台中的一些数据。我只是无法将代码输出到我的表单元素。

所以我让Axios拦截响应并获取数据

  methods: {

    },
    mounted: function() {
        axios.interceptors.response.use(function (response) {
            return response.data;
        });
    },
    destroyed: function() {

    }
}

所以这是挂载在页面加载和我的数据

 data() {
            return {
                formpersonaldetails: {},

                title: this.results.title,
                titleoptions: ['Mr', 'Mrs', 'Miss', 'Ms'],
                fname: this.results.fname,
                sname: this.results.sname,
                dob: this.results.dob, 

你猜哪个不起作用,所以我哪里出错了?

响应如此:

{"id":2,"user_id":null,"fname":"Graham","sname":"Morby-Raybould","dob":"1982-07-10T08:22:00.000Z","gender":"Male","nationality":"British","mobile":null,"telephone":null,"hname":"","address2":"","address3":"","postcode":"","noktitle":"","nokfname":"","noksname":"","nokcontactnumber":"","nokhname":"","nokaddress2":"","nokaddress3":"","nokpostcode":"","emp_type":"","trade":"","ninumber":"","utrnumber":"","vatreg":null,"vatcert":"","created_at":"2017-10-10 08:22:37","updated_at":"2017-10-10 08:22:37"}

1 个答案:

答案 0 :(得分:0)

您需要将response传递给function

axios.interceptors.response.use(function (response) {
    // Do something with response data
    return response;
  }, function (error) {
    // Do something with response error
    return Promise.reject(error);
  });

此处提供更多信息:Axios