角离子:属性“ json”在类型“对象”上不存在

时间:2018-07-20 06:12:28

标签: angular http ionic-framework ionic3

我正在创建一个Ionic 2项目,该项目显示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

2 个答案:

答案 0 :(得分:2)

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

imports:[
  HttpClientModule
]

答案 1 :(得分:0)

您的错误是= [没有HttpClient的提供者!]  当您未在 app.modules.ts 文件中定义http模块时,会发生此类错误。   所以先检查一下。

exp:   从'@ angular / http'导入{HttpModule};

导入:[HttpModule]