我正在创建一个Ionic2项目,展示GitHub用户及其详细信息。
我已经创建了名为github-users.ts的服务文件,编译器显示了这个错误
在.json()方法中。
属性“jSON”不存在于“对象”类型上
这里是我的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

最佳答案

您需要将httpclientmodule导入imports下的app.module.ts

imports:[
  HttpClientModule
]

10-05 22:55
查看更多