【问题标题】:how to append axios data with good table in vue如何在vue中用好表附加axios数据
【发布时间】:2018-09-10 05:03:23
【问题描述】:

伙计们,我是 Vue 新手,所以不知道如何解决这个问题让我先显示代码然后问题,我正在使用 Vue-Good-table 这里是链接https://xaksis.github.io/vue-good-demos/#/simple-table

<vue-good-table
  title="Product List"
  :columns="columns"
  :rows="rows"
  :paginate="true"
  :lineNumbers="true" />

export default {
data(){
return {
  columns: [
    {
      label: 'productname',
      field: 'product_name',
      filterable: true,
    },
    {
      label: 'productdesc',
      field: 'product_desc',
     // type: 'number',
      html: false,
      filterable: true,
    },

  ],
  rows:[],//get axios return value
 };
},
 methods:{
  next() {
    var _this=this
     this.$http.get('http://localhost:3000/api/companyproducts')
            .then(function (response) {

           _this.rows=response.data
            })
            .catch(function (error) {

                });
    }
  }
}

现在我的问题是如何将这个 axios 响应值附加到好表的行中

【问题讨论】:

  • 我会提到你正在用这个调用替换表格的内容,而不是追加。要追加,请使用rows.push( response.data ) 方法。

标签: vue.js vue-tables-2


【解决方案1】:

要“附加 axios 数据”,您必须先在项目中安装 axios。

所以首先将 axios 安装到你的项目中:

npm install --save axios

然后将下一个方法转换为以下方法:

  next() {
    axios.get('http://localhost:3000/api/companyproducts')
      .then(response => {
         this.rows = response.data
      }) 
      .catch(error => console.log(error))
  }

注意,你还必须导入 axios。所以当脚本标签开始使用时:

import axios from 'axios'

Axios 是一个很棒的软件包,你可以了解更多 here

正如我在你的代码中看到的,你没有使用 axios,但 vue-resource.Vue-resource 也是一个很好的包,所以你可以了解更多here

【讨论】:

    【解决方案2】:

    我猜你有代码,http.get 完成后视图没有响应。

    您可以通过将 rows 配置为计算属性来做到这一点,因为这会监视它的依赖项(即 row_data)以进行更改

    computed: {
      rows: function () { 
        return this.row_data;
      }
    }
    ...
    data(){
      return {
        columns: [
        ...
        ],
      row_data:[]
     };
    }
    ...
    methods: {
      next() {
        var _this=this
        this.$http.get('http://localhost:3000/api/companyproducts')
          .then(function (response) {
             _this.row_data = response.data
          })  
      }
    }
    ...
    created: function() {
      this.next()
    }
    

    【讨论】:

    • next() 方法没有得到触发,因此我没有得到数据
    • 好的,你知道lifecycle hooks吗?您可以在 created() 挂钩中触发next()。
    猜你喜欢
    • 2021-09-06
    • 2021-12-23
    • 2020-04-18
    • 2019-12-11
    • 2021-10-20
    • 2020-04-21
    • 2019-05-24
    • 1970-01-01
    • 2021-07-22
    相关资源
    最近更新 更多