问题描述
每个Axios method都使用generic types,因此您可以执行以下操作:
axios
.get<countries>('https://func-myportal-dev.azurewebsites.net/api/GetCountries?code=j0xGK8L6iRXpZqR3DgaDRoI/H/qjGZSGdBPIY9rZU84uH4SUa80noQ==')
.then(res => { /* res.data here is countries type */ })
解决方法
在setState上,我收到以下TS错误:
“类型'AxiosResponse‘缺少类型’countries[]‘的以下属性:长度、pop、push、concat和26 more.ts(2740)”
谁能建议一下哪里出了问题/在哪里使用:countries[]?
export interface IProfileEditorState {
countries: countries[];
}
interface countries {
countryID: string;
countryName: string;
label: astring;
value: string;
isDirector: number;
}
// Temp
public getCountrys() {
axios
.get('https://func-myportal-dev.azurewebsites.net/api/GetCountries?code=j0xGK8L6iRXpZqR3DgaDRoI/H/qjGZSGdBPIY9rZU84uH4SUa80noQ==')
.then(res => {
this.setState({countries: res});
})
// Error catching
.catch(error => this.setState({ error, isLoading: false }));
}