【问题标题】:Nuxt + axios ...how to add {{this.$route.params}} in axios url?Nuxt + axios ...如何在axios url中添加{{this.$route.params}}?
【发布时间】:2021-02-23 07:15:21
【问题描述】:

您好,我正在尝试将 {{this.$route.params}} 添加到我的 axios url 以获取专用配置文件页面上的数据。 这是我想在 axios url 中添加“id”的部分:

'RikLamers' 的 Axios 代码向我解释了如何获取数据,这是我要添加“this.$route.params”的地方:

 data() {
  return {
    results: [],
  }
},
 async  mounted() {
  await axios.get(`API url" ${to.params.id}`)
.then(response => {this.results = response.data.content})

 }, 

但我在某个地方弄错了,希望有人能解释一下吗?

【问题讨论】:

  • 是否要根据路由参数获取数据?
  • 是的,就是这样 :) 我需要像 aurelie-wulff-2 这样将“id”添加到我的 axios get url
  • 酷,下面的答案会起作用!

标签: vue.js axios nuxt.js


【解决方案1】:

您可以使用 this.$route.params.id 访问 URL 参数。当你使用await时,使用try catch。

data() {
  return {
    results: [],
  }
},
async  mounted() {
  try {
    const response = await axios.get(`[Your API URL]/${this.$route.params.id}`)
    this.results = response.data.content
  catch (e) {}
},

【讨论】:

  • 谢谢你我现在正在尝试,但是我收到了一个 catch 语法错误的错误
  • 也许我忘记关闭它了?捕捉 (e) {} },
  • 感谢 Prime 工作得很好,我只需添加 this.results = response.data.content} catch (e) {console.error(error)} },不再出现错误...再次感谢
猜你喜欢
  • 2021-08-19
  • 2021-03-05
  • 2018-11-06
  • 2021-08-25
  • 2019-01-24
  • 2020-01-21
  • 2020-07-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多