如何使用VueJ和Axios在表中显示所有用户?

时间:2019-06-20 08:23:42

标签: node.js api vue.js

我正在尝试将用户从数据库(MYSQL)显示到vueJs中的表。 在下面的代码中,我尝试在表中显示用户,但未显示任何内容。另外,当我访问 console.log 时,我可以看到

  • Array(4)0:{…} 1:{…} 2:{…} 3:{…}

,每次单击0:{…},我都会看到JSON。我应该怎么做才能在表中显示此代码。

<template>
<div class="container">
<div class="row">
  <div class="col-md-8 col-md-offset-2">
    <div class="panel panel-default">
      <div class="panel-heading">List of users</div>
      <div class="panel-body">
        <table class="table">
          <thead>
            <tr>
              <th>Firstname</th>
              <th>Lastname</th>
              <th>Email</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="user in users" :key="user.id">
              <td>user.url</td>
              <td>user.lastaname</td>
              <td>user.email</td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>

  

和脚本中

<script>
import QrcodeVue from "qrcode.vue";
import axios from "axios";

export default {
  data() {
    return {

      users: [],

    };
  },
  methods: {
    getUsers() {
      axios
        .get("localhost:2000/user/")
        .then(response => (this.users = response.data))
        .catch(error => console.log(error.message));

  },
  components: {
    QrcodeVue
  },
  mounted() {
    axios
      .get("localhost:2000/user/")
      .then(response => {
        this.results = response.data[1];
        console.log(this.results);
      })
      .catch(err => {
        throw err;
      });
  }
};
</script>

感谢您的帮助!

PS

我尝试遵循在StackOverflow中也被问到的code

编辑:这是输出now

1 个答案:

答案 0 :(得分:2)

您忘记了花括号!

以下是代码:

<div class="container">
<div class="row">
  <div class="col-md-8 col-md-offset-2">
    <div class="panel panel-default">
      <div class="panel-heading">List of users</div>
      <div class="panel-body">
        <table class="table">
          <thead>
            <tr>
              <th>Firstname</th>
              <th>Lastname</th>
              <th>Email</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="user in users" :key="user.id">
              <td>{{ user.url }}</td>
              <td>{{ user.lastname }}</td>
              <td>{{ user.email }}</td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>