【问题标题】:Angular6 - 'json' does not exist on type 'Object' [duplicate]Angular6 - '对象'类型上不存在'json' [重复]
【发布时间】:2019-01-09 16:08:44
【问题描述】:

我在 Angular 6 中收到以下函数的以下错误

public getList() {
        return this.http.get(environment.serverUrl +'/vmsdata/v1/tftoport/customer/1001')
            .toPromise()
            .then(response => response.json()
            .then(json => {
                console.log('data', json.items);
                return json.items;
            });
    }

对于.then(response => response.json() 行,错误是 [ts] Property 'json' does not exist on type 'Object'.

对于.then(json => { 行,错误为[ts] Parameter 'json' implicitly has an 'any' type.

【问题讨论】:

  • 您的代码中至少缺少一个右括号)
  • this.httpHttpHttpClient 的实例吗?您可以通过将json => ... 更改为(json: any) => ... 来消除第二个错误,或者您可以在 tsconfig.json 中禁用 noImplicityAny
  • 在 Angular 6 中,旧的、已弃用的 Http 类不再存在,因此它必然是 HttpClient;默认情况下将检索到的数据转换为 json。所以你不需要.then(response => response.json())

标签: javascript json angular angular6


【解决方案1】:

在 Angular 6 中,您不需要将数据映射到 json,因为 Angular 6 默认提供 json。

【讨论】:

    猜你喜欢
    • 2023-03-08
    • 2019-06-20
    • 2018-02-10
    • 2018-05-13
    • 1970-01-01
    • 2019-01-25
    • 2019-03-20
    • 2021-09-19
    • 1970-01-01
    相关资源
    最近更新 更多