【问题标题】:Axios Response data and Vue JS , Using the dataAxios 响应数据和 Vue JS , 使用数据
【发布时间】:2018-03-22 19:41:33
【问题描述】:

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"}

【问题讨论】:

  • 它不起作用可能是因为当您收到来自 axios 的响应时,您没有做任何进一步的事情。您可能希望将响应数据映射到组件的数据对象中,但当然,由于我们不知道您的响应是什么,因此我们无法告诉您如何将该响应合并到您预先存在的数据中。
  • 我会在特里的帖子中添加我的回复

标签: javascript vue.js vuejs2 axios


【解决方案1】:

您需要将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

【讨论】:

  • 所以我已经改变了,你将如何在数据中使用它?
  • 您将响应数据存储在哪里?
猜你喜欢
  • 2020-08-19
  • 2021-08-11
  • 1970-01-01
  • 2019-04-25
  • 1970-01-01
  • 2017-12-26
  • 2018-12-03
  • 2019-06-20
  • 2018-12-05
相关资源
最近更新 更多