【问题标题】:Axios.delete(url[,config]): Type has no properties in common with type 'AxiosRequestConfig'Axios.delete(url[,config]):类型与类型“AxiosRequestConfig”没有共同的属性
【发布时间】:2020-06-14 08:59:43
【问题描述】:

我使用 React、Typescript 和 Axios。我声明一个类由这样的静态函数填充:

import axios from "axios"

export default class Users {

  static checkinByPassword(username: string, password: string){
    const params = {username, password}
    return axios.post(`/${this._key}/checkinbypassword`, params)
  }

  static delete(id: string){
    const params = {id: id}
    return axios.delete(`/${this._key}`, params)
  }
}

第一个函数 (checkinByPassword) 工作正常。第二个函数使 ESLint(我使用 ESLint 进行 VSCode 编辑器)抛出错误:

Type '{ id: string; }' has no properties in common with type 'AxiosRequestConfig'.

AxiosRequestConfig 是什么?以及如何使我的 params 对象与之兼容? 提前谢谢你

【问题讨论】:

    标签: reactjs typescript axios eslint typescript-eslint


    【解决方案1】:

    axios.delete 有两个参数,第一个是 url 路径,第二个是配置。

    您需要将您的 params 对象包装到另一个具有 data 属性的对象。

    例如:

    const config = {
      data: {
        id: "your id"
      }
    }
    
    axios.delete(url, config)...
    

    const params = {id: id};
    
    axios.delete(url, {
      data: params
    })...
    

    【讨论】:

    • 嗯......它的工作!但在这里:github.com/axios/axios#note 他们说When using the alias methods url, method, and data properties don't need to be specified in config。据我所知,.delete() 方法是别名之一
    • 嗯..是的。也许你是对的,或者作者想说点别的。顺便说一句,我建议你继续编码。
    猜你喜欢
    • 2023-01-05
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    • 2022-08-10
    • 2020-03-29
    相关资源
    最近更新 更多