【问题标题】:Axios get method wont display any resultsaxios get 方法不会显示任何结果
【发布时间】:2020-08-27 17:23:28
【问题描述】:

我正在尝试从 json 文件中获取用户数据。我正在使用 axios get 方法。它不会在我的页面上显示任何内容。它只是空白。谁能帮帮我?

.users(v-for="user in users")
    p {{ user.name }}

data ()  {
    return {
      users: []
    }
  },
  methods: {
    fetchUsers() {
      axios.get('https://jsonplaceholder.typicode.com/users')
      .then((response) => {
        const data = response.data;
        this.users = data
      })
      .catch((error) => {
        console.log(error);
      })
    }
  },

【问题讨论】:

  • 你能分享更多代码吗?你有没有调用过你的方法?

标签: vue.js vuejs2 axios


【解决方案1】:

您必须调用fetchUsers 方法。在created 部分调用它。

created() {
    this.fetchUsers();
  }

【讨论】:

  • 谢谢。我忘了这件事。 mounted: function () {}下也可以这样用吗?
  • 绝对可以
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多