【问题标题】:Nuxt - asyncData ERROR: "Binding element '$axios' implicitly has an 'any' type."Nuxt - asyncData 错误:“绑定元素 '$axios' 隐式具有 'any' 类型。”
【发布时间】:2021-01-13 12:26:34
【问题描述】:

在此处关注文档:https://axios.nuxtjs.org/setup

我在运行yarn dev 时将此错误记录到控制台:

ERROR  ERROR in pages/index.vue:51:21                                                                                                                                                                                                                                                                           23:01:53
TS7031: Binding element '$axios' implicitly has an 'any' type.
    49 |   }
    50 | 
  > 51 |   async asyncData({ $axios }) {
       |                     ^^^^^^
    52 |     const ip = await $axios.get('http://icanhazip.com')
    53 |     return { ip }
    54 |   }

我已经三重检查了我的nuxt.config.jstsconfig.json 中有'@nuxtjs/axios'

我错过了什么?它在第一次工作时出现了这个错误,我不知道为什么?!

任何帮助表示赞赏,谢谢!

【问题讨论】:

  • 你的tsconfig.json中有@nuxt/types吗?
  • 感谢您的评论。是的,我确实在里面。 :-/

标签: typescript vue.js async-await nuxt.js typeerror


【解决方案1】:

asyncData 被传递了一个名为 context 的参数,该参数在 @nuxt/types 中有一个对应的类型,名为 Context。从@nuxt/types 导入Context,然后像这样声明解构参数的类型:

async asyncData({ $axios } : Context) {
  const ip = await $axios.get('http://icanhazip.com')
  return { ip }
}

【讨论】:

    猜你喜欢
    • 2020-06-19
    • 2023-01-28
    • 1970-01-01
    • 2021-08-26
    • 2023-02-07
    • 2022-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多