【问题标题】:Axios Vue Js: How to get the value of this object to show in api get request urlAxios Vue Js:如何获取此对象的值以显示在 api get request url
【发布时间】:2020-10-07 15:03:35
【问题描述】:

这是我的 vue 文件,它接受表中的 id。这行得通,我可以从表格行中获取数据 id

  showProd (id) {
   Products.showProd().then((response) => {
     console.log(show1prod.product_name)
   })
   .catch((error) => {
     alert(error)
   })

这是我调用 axios.get 的配置文件,我可以到达后端但不生成查询,因为这个 url/api 发送的对象我相信不是 id 号

export default {
    async showProd(id) {
        return Api.get('/products/{id}',id)
    },

    loadProds () {
        return Api.get('/products')
    }

}

【问题讨论】:

  • 看起来参数不匹配。你传递了“props”,但在async showProd(props) 中使用了“id”
  • 对不起,这只是我弄错了,但它仍然不起作用,在网络选项卡的开发工具中,这是 id "%7Bid%7D" 的值而不是数字值“身份证”

标签: axios vue-router laravel-vue


【解决方案1】:

首先确保您的 API 调用正确:

export default {
    showProd(id) { // async not needed, axios returns promise
        return axios.get('/products/' + id)
    },

    loadProds () {
        return axios.get('/products')
    }
}

你可以调用这些函数:

showProd (id) {
   Products.showProd(id).then((response) => { // Note the id in the call
     console.log(response.data.product_name) // Use the response object
   })
   .catch((error) => {
     alert(error)
   })

【讨论】:

  • 天哪,它有效,我不敢相信这个“​​+”就是我所缺少的我整晚都在燃烧我的眉毛,无论如何,我会用你的方法解决这个问题回答并感谢您花时间回答这个问题,您是天使
  • 最后一个问题如何将其显示给输入?我尝试将响应分配给属性 show1prod 并尝试调用类似 show1prod.product_name 的数据 我假设第二个属性是数据库中的名称字段,在模板中显示这个的正确方法是什么?喜欢绑定这个吗?
猜你喜欢
  • 1970-01-01
  • 2023-02-07
  • 2019-08-18
  • 2019-08-03
  • 2018-07-07
  • 2021-09-22
  • 2018-04-01
  • 2020-10-21
  • 2019-08-03
相关资源
最近更新 更多