Angular ionic : Property 'json' doesn't exist on type 'Object'

标签 angular http ionic-framework ionic3

我正在创建一个显示 github 用户及其详细信息的 Ionic 2 项目。 我创建了名为 github-users.ts 的服务文件,编译器向我显示了这个错误 在 .json() 方法中。

Property 'json' doesn't exist on type 'Object'

这是我的github-users.ts源文件

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Rx';
import "rxjs/add/operator/map";

import { User } from "../../models/user";

@Injectable()
export class GithubUsersProvider {
  githubApiUrl = 'api.github.com';

  constructor(public http: HttpClient) { 
    console.log("Hello Github Users service");
   }

  load(): Observable<User[]> {
    return this.http.get(`${this.githubApiUrl}/users`)
      .map(res => <User[]>res.json());
  }
}

我从控制台收到这个错误

core.js:1449 ERROR Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[GithubUsersProvider -> HttpClient]: 
  StaticInjectorError(Platform: core)[GithubUsersProvider -> HttpClient]: 
    NullInjectorError: No provider for HttpClient!
Error: StaticInjectorError(AppModule)[GithubUsersProvider -> HttpClient]: 
  StaticInjectorError(Platform: core)[GithubUsersProvider -> HttpClient]: 
    NullInjectorError: No provider for HttpClient!
    at _NullInjector.get (core.js:1003)
    at resolveToken (core.js:1301)
    at tryResolveToken (core.js:1243)
    at StaticInjector.get (core.js:1111)
    at resolveToken (core.js:1301)
    at tryResolveToken (core.js:1243)
    at StaticInjector.get (core.js:1111)
    at resolveNgModuleDep (core.js:10896)
    at _createClass (core.js:10933)
    at _createProviderInstance$1 (core.js:10907)
    at _NullInjector.get (core.js:1003)
    at resolveToken (core.js:1301)
    at tryResolveToken (core.js:1243)
    at StaticInjector.get (core.js:1111)
    at resolveToken (core.js:1301)
    at tryResolveToken (core.js:1243)
    at StaticInjector.get (core.js:1111)
    at resolveNgModuleDep (core.js:10896)
    at _createClass (core.js:10933)
    at _createProviderInstance$1 (core.js:10907)
    at c (polyfills.js:3)
    at Object.reject (polyfills.js:3)
    at NavControllerBase._fireError (nav-controller-base.js:223)
    at NavControllerBase._failed (nav-controller-base.js:216)
    at nav-controller-base.js:263
    at t.invoke (polyfills.js:3)
    at Object.onInvoke (core.js:4760)
    at t.invoke (polyfills.js:3)
    at r.run (polyfills.js:3)
    at polyfills.js:3

最佳答案

您需要在 imports 下将 HttpClientModule 导入到您的 app.module.ts

imports:[
  HttpClientModule
]

关于 Angular ionic : Property 'json' doesn't exist on type 'Object' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51436091/

相关文章:

angular - 如何关闭使用 Angular 播放器嵌入JW Player的YouTube视频末尾显示为缩略图的建议视频?

angular - Ionic 3 网格无法将行项目垂直居中

javascript - ionic 有标题+ ionic 侧菜单不起作用

angular - 如何在 ionic 2 中使用 pdfmake?

javascript - 在 Angular 8 应用程序中使用外部 JavaScript 库

angular - 将授权 header 添加到 Springfox

Angular 动态模块提供者

http - 如何在 Dart 中设置多部分请求的超时?

angularjs - 我们可以将 $http 注入(inject) Angular 服务并将该数据传递给 Controller ​​吗?

http - panic : runtime error: invalid memory address or nil pointer dereference with bigger data