简体   繁体   English

Angular Service 和 HttpClient 类型不存在

[英]Angular Service and HttpClient Type does not exist

I am trying to get a service working in Angular 5. This is what I have:我正在尝试让服务在 Angular 5 中工作。这就是我所拥有的:

import { Injectable } from '@angular/core';


@Injectable()
export class DataService {

  constructor() { }

  getData() {
    // don't use 'any', type your data instead!
    return this.httpClient.get<any>('./assets/data.json');
  }

}

I'm getting the following error:我收到以下错误:

Property HttpClient does not exist on type DataService.属性 HttpClient 在类型 DataService 上不存在。

What am I missing?我错过了什么?

You need to import HttpClient from the module like below您需要从如下模块中导入 HttpClient

import {HttpClient} from '@angular/common/http';

and you code should be like this你的代码应该是这样的

import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http';

@Injectable()
export class DataService {

  constructor(private httpClient: HttpClient) { }

  getData() {
    // don't use 'any', type your data instead!
    return this.httpClient.get<any>('./assets/data.json');
  }

}

Try like this :像这样尝试:

readmore about httpClient here在此处阅读有关 httpClient 的更多信息

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable()
export class DataService {

  constructor(private httpClient: HttpClient) { }

  getData() {
    // don't use 'any', type your data instead!
    return this.httpClient.get<any>('./assets/data.json');
  }

}

Consider updating the code as follows:考虑按如下方式更新代码:

import { Injectable } from '@angular/core';
import { Http } from "@angular/http";

@Injectable()
export class DataService {

  constructor(private httpClient: Http) { }

  getData() {
    // don't use 'any', type your data instead!
    return this.httpClient.get<any>('./assets/data.json');
  }

}

Let me know if it works.让我知道它是否有效。

Http Calls have been modified and moved to '@angular/common/http' as HttpClient, since the idea was supported by many developers since its move to '@angular/common/http' from Angular 4.3 and further imports of Http Module from @angular/http is deprecated from Angular 5.Check out the changes here : https://jaxenter.com/road-to-angular-5-133253.html . Http Calls 已被修改并移至 '@angular/common/http' 作为 HttpClient,因为该想法自 Angular 4.3 移至 '@angular/common/http' 并进一步从 @ 导入 Http 模块以来得到了许多开发人员的支持angular/http 已从 Angular 5 中弃用。在此处查看更改: https ://jaxenter.com/road-to-angular-5-133253.html。

Now to your question, you should import HttpClient Module and create an instance of it.现在回答您的问题,您应该导入 HttpClient 模块并创建它的一个实例。 So your code would be like:-所以你的代码会是这样的:-

import { Injectable } from '@angular/core';从“@angular/core”导入{可注射}; import {HttpClient} from '@angular/common/http';从 '@angular/common/http' 导入 {HttpClient};

@Injectable() export class DataService { @Injectable() 导出类 DataService {

constructor(private _httpClient: HttpClient) { }

getData() {
    // don't use 'any', type your data instead!
    return this._httpClient.get<any>('./assets/data.json');
}

} }

For me the problem was that I didn't have the httpClient listed as private in my constructor.对我来说,问题是我的构造函数中没有将 httpClient 列为私有

Original Constructor:原始构造函数:

constructor(
  httpClient: HttpClient
) { }

Updated Constructor that resolved the error:更新了解决错误的构造函数:

constructor(
  private httpClient: HttpClient
) { }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ionic / Angular HttpClient没有获取类型数据:错误类型“ {}”上不存在属性“名称” - Ionic / Angular HttpClient getting no type data : error Property 'name' does not exist on type '{}' 角度中的“ArrayBuffer”类型不存在属性“split” - Property 'split' does not exist on type 'ArrayBuffer' in angular Angular ~“对象”类型上不存在属性“” - Angular ~ Property '' does not exist on type 'Object' Angular 类型“从不”上不存在属性“内容” - Angular Property 'content' does not exist on type 'never' Angular 类型“ExpenseEntryComponent”上不存在属性“expenseEntry” - Angular Property 'expenseEntry' does not exist on type 'ExpenseEntryComponent' 属性“ campanha”在类型“ DisputaComponent []”上不存在-Angular 2 - Property 'campanha' does not exist on type 'DisputaComponent[]' - Angular 2 类型“ AbstractControl”上不存在属性“控件”。 角度7 - Property 'controls' does not exist on type 'AbstractControl'. Angular 7 Angular 13 属性在类型“FormGroup”上不存在 - Angular 13 Property does not exist on type 'FormGroup' Angular 8 属性在 AppComponent 类型上不存在 - Angular 8 property does not exist on the type AppComponent 'void'类型上不存在属性'管道' Angular 9 - Property 'pipe' does not exist on type 'void' Angular 9
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM