【问题标题】:accessing the json data from axios call in vue从 vue 中的 axios 调用访问 json 数据
【发布时间】:2019-05-17 07:14:12
【问题描述】:

我在邮递员中设置了一个假的 mocke 服务器来获取我的 vue 应用程序的 som 测试数据。这是我从服务器得到的:

我想访问数据信息,但我很难做到。这是我正在使用的代码:

<template>
  <div class="admin">
    <h1>This is admin page area</h1>

    <JsonEditor :objData="config" v-model="config" ></JsonEditor>

  </div>
</template>

<script>

import{getConfig} from "../utils/network";

import Vue from 'vue'
import JsonEditor from 'vue-json-edit'

Vue.use(JsonEditor)

export default{

  data: function () {
    return {
      config:{}
    }
  },
  created:function(){
    getConfig()
            .then(response => {
              console.log(response)
              this.config = response;
            })
            .catch(err => {
              console.log(err)
            })
  }
}
</script>

我尝试过response.data,但没有成功。我正在将它提供给 json 编辑器,但它目前无法显示 json。它是空的。这是我的网络调用代码:

import Vue from 'vue'
import axios from 'axios'
import VueAxios from 'vue-axios'

Vue.use(VueAxios, axios)

const getConfig = () =>{
    return Vue.axios.get('https://8db51a11-752e-4d64-b237-8195055fbf26.mock.pstmn.io')
};

//Export getConfig so other classes can use it
export{
  getConfig
}

我错过了什么?

【问题讨论】:

  • 你应该使用async/await

标签: json vue.js axios


【解决方案1】:

您也可以尝试提供 then 语句:

const getConfig = () =>{
    Vue.axios.get('https://8db51a11-752e-4d64-b237-8195055fbf26.mock.pstmn.io').then(response => {
return response.data});
};

【讨论】:

    猜你喜欢
    • 2020-05-28
    • 2017-12-26
    • 2019-05-12
    • 2021-01-04
    • 2021-07-24
    • 2023-03-23
    • 2021-11-02
    • 1970-01-01
    • 2020-01-06
    相关资源
    最近更新 更多