gpt4 book ai didi

Angular6 升级问题 : Property 'data' does not exist on type 'Object'

转载 作者:行者123 更新时间:2023-12-04 12:08:07 26 4
gpt4 key购买 nike

我正在将我的 angular 应用程序从 v5 升级到 7。

我已经完成了 Angular 更新指南中提到的所有迁移步骤。
但是我的现有代码面临一个问题。

myservice.service.ts

import {Injectable, Inject} from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {Response, Headers, RequestOptions} from "@angular/http";

@Injectable()
export class MyApiService{
constructor(private http: HttpClient, @Inject(MY_HOST) private host: string)
{
this.host = this.host + "/api/common";
}
getNotification (appName) {
return this.http.get(this.host + "/notifications")
}
}

我的component.component.ts
import {combineLatest as observableCombineLatest, Subject, Observable, Subscription} from 'rxjs';
import {MyApiService} from "../../shared/services/myservice.service";

@Component({..// template and style url...});

export class NotificationComponent implements OnInit{
constructor(private myApiService: MyApiService)

getNotification(): void {
this.myApiService.getNotification('myApp').subscribe(response => {
console.log(response.data); **// ERROR: It throws error here. Property** 'data' does not exist on type 'Object'.
}, (error: void) => {
console.log(error)
})
}

}

最佳答案

您必须使用 any或自 data 以来的自定义响应类型类型不存在 {} :

.subscribe((response: any) => ...)
自定义响应接口(interface)是最好的解决方案:
export interface CustomResponse {
data: any;
}

.subscribe((response: CustomResponse) => ...)
请注意,您还可以使用这样的类型:
this.httpClient.get<CustomResponse>(...)
.subscribe((response) => ...) // response is now CustomResponse

关于Angular6 升级问题 : Property 'data' does not exist on type 'Object' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52588576/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com