首页 > 解决方案 > 我想在axios get方法中将id调用到兄弟组件中,并在vue客户端表中显示输出数据

问题描述

 created () {
   let self = this; 

   // after this point, `self` will reference the Vue instance even in callbacks
  this.$root.$on('eventing', tableDataId => {
    axios.get('http://192.168.2.35:8000/api/v1/post_bi' + '/' + tableDataId)
      .then((response) => {
        // setting `self` to `this` here doesn't make sense because `this`
        // is not refering to the Vue instance in this callback
        self.tableData = response.data;
        console.log(self.tableData)
      })
      .catch((error) => {
        console.log(error)
      })
  })

我写了这段代码,但是表数据的输出没有显示在 v 客户端表中。

标签: vue.js

解决方案


尝试这个

created () {
   axios.get('http://192.168.2.35:8000/api/v1/post_bi/'+ this.tableDataId)
     .then((response) => {
       this.tableData = response.data;
     })
     .catch((error) => {
       console.log(error)
   })
})

假设您已经像这样在组件中声明了 tableData 和 tableDataId

data() {
  return {
    tableData: '',
    tableDataId: '',
  }
}

推荐阅读