【问题标题】:Vue.js data table only showing data in first columnVue.js 数据表仅显示第一列中的数据
【发布时间】:2019-01-11 22:11:13
【问题描述】:

我正在尝试遍历使用 axios 从数据库中提取的对象。我可以让对象显示在我的数据表中,但我无法让它将数据分解到指定的列

第一个 sn-p 是父组件。我将实际列表的 tr 和 td 拆分为一个单独的组件,但这可能需要修复。

<template>
<div class="container">
<router-link to="/add" class="btn btn-primary btn-sm float- 
right">AddClient</router-link>
<table class="table">
<thead class="thead-dark">
  <tr>
      <th scope="col">#</th>
      <th scope="col">Name</th>
      <th scope="col">Type</th>
      <th scope="col">Email</th>
      <th scope="col">Phone</th>
      <th></th>
  </tr>
</thead> 
<client-info></client-info>      
</table>

</div>
</template>

<script>
import ClientInfo from '@/components/clientslistexperience/ClientInfo'


export default {
name: 'ClientsList',
components: {
ClientInfo
},
methods: {

},

}
</script>

enter code here

接下来是遍历要在表格中显示的数据的组件

<template>
<div class="client-info">
    <tbody>
        <tr v-for="(client, index) in clients"  v-bind:key="index"  
 :client="client">
            <th scope="row">{{ client.id }}</th>
            <td>{{ client.name }}</td>
            <td>{{ client.type }}</td>
            <td>{{ client.email }}</td>
            <td>{{ client.phone }}</td>
        </tr>
    </tbody>
</div>

从'vuex'导入{ mapState}

  export default {
name: 'client-info',
props: {
    id: Array,
    type: String,
    name: String,
    email: String,
    phone: Number, 
},
computed: {
    ...mapState ([
        'clients'
    ])
},
created() {
this.$store.dispatch('retrieveClients')
}

}
</script>
enter code here

last 是发出 axios 请求的 vuex 存储。现在我知道将 vuex 用于较小的项目已经过时了,但我打算让它变得相当大,所以这是我选择的方法。任何帮助都是极好的!谢谢。

  import Vue from 'vue'
  import Vuex from 'vuex'
  import axios from 'axios'

   Vue.use(Vuex)
   axios.defaults.baseURL = 'http://client-api.test/api'

   export default new Vuex.Store({
   state: {
    clients: []
   },
   mutations: {
    retrieveClients(state, clients) {
      state.clients = clients
    },
   },
   actions: {
    retrieveClients(context) {
      axios.get('/clients')
      .then(response => {
        // console.log(response)
        context.commit('retrieveClients', response.data)
      })
      .catch(error => {
        console.log(error)
      })
    }
    }
    })

enter image description here

【问题讨论】:

    标签: javascript vue.js vue-component vuex


    【解决方案1】:

    实际上,您需要删除 ClientInfo.vue 组件中的 div 根元素。 将div 替换为tbody 即可解决您的问题;)

    <template>
          <tbody class="client-info">
            <tr v-for="(client, index) in clients"  
              :key="index">
                  <td>{{ client.id }}</td>
                  <td>{{ client.name }}</td>
                  <td>{{ client.type }}</td>
                  <td>{{ client.email }}</td>
                  <td>{{ client.phone }}</td>
              </tr>
          </tbody>
    </template>
    

    完整演示在这里>https://codesandbox.io/s/v8vw0z89r7

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 2021-02-13
      相关资源
      最近更新 更多