我用方法上了课:
export default class ChatService {
static async findDMChat(companionID: number) {
return $api.post<IChat>('/findDMChat', {companion_id: companionID})
}
}在ChatStore.setCompanion( )中,我希望将该方法的结果分配给属性:
export class ChatStore {
companion = {} as IUser;
chat = {} as IChat;
constructor() {
makeAutoObservable(this)
}
async setCompanion(companion: IUser) {
this.companion = companion;
this.chat = await ChatService.findDMChat(companion.id) // Type 'AxiosResponse<IChat, any>' is missing the following properties from type 'IChat': type, idts(2739)
}代码注释中显示了错误的描述。提前谢谢。
发布于 2022-09-03 14:08:13
await ChatService.findDMChat(companion.id)返回响应的结果,而不仅仅是数据。如果您查看AxiosResponse类型,您将看到响应包括什么
export interface AxiosResponse<T = any, D = any> {
data: T;
status: number;
statusText: string;
headers: AxiosResponseHeaders;
config: AxiosRequestConfig<D>;
request?: any;
}因此,在您的示例中,您应该从响应中获得data属性
const { data} = await ChatService.findDMChat(companion.id);
this.chat = data;https://stackoverflow.com/questions/73592798
复制相似问题