财产'得到'类型' HttpClientModule'

时间:2017-11-18 18:57:06

标签: angular


对于Angular js来说很新,试着从youtube上学习它,我在代码中遇到了提到的问题,我不明白我的代码中缺少什么

ERROR

我正在"财产'得到'在类型' HttpClientModule'"中不存在尝试访问HttpClientModule的获取服务时出错。,
以下是代码详情
app.component.ts文件是

import { Component,OnInit  } from '@angular/core';
   import {HttpClientModule} from '@angular/common/http';
     @Component({
   selector: 'app-root',
   templateUrl: './app.component.html',
   styleUrls: ['./app.component.css']
   })
   export class AppComponent {

   constructor(private http:HttpClientModule){};
   title = 'app';
   ngOnInit() {
   this.http.get('https://my-json-server.typicode.com/techsithgit/json-faker-
   directory/profiles').
    subscribe((data)=>{
      console.log(data);
    });

   }

}

app.module.ts文件是

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import {HttpClientModule} from '@angular/common/http';

import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Package.json文件是

{
  "name": "trust",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^5.0.2",
    "@angular/common": "^5.0.2",
    "@angular/compiler": "^5.0.2",
    "@angular/compiler-cli": "^5.0.2",
    "@angular/core": "^5.0.2",
    "@angular/forms": "^5.0.2",
    "@angular/http": "^5.0.2",
    "@angular/platform-browser": "^5.0.2",
    "@angular/platform-browser-dynamic": "^5.0.2",
    "@angular/platform-server": "^5.0.2",
    "@angular/router": "^5.0.2",
    "core-js": "^2.4.1",
    "rxjs": "^5.4.2",
    "typescript": "^2.6.1",
    "zone.js": "^0.8.18"
  },
  "devDependencies": {
    "@angular/cli": "1.4.9",
    "@angular/compiler-cli": "^4.2.4",
    "@angular/language-service": "^4.2.4",
    "@types/jasmine": "~2.5.53",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "~3.2.0",
    "jasmine-core": "~2.6.2",
    "jasmine-spec-reporter": "~4.1.0",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.1.1",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~3.2.0",
    "tslint": "~5.7.0",
    "typescript": "~2.3.3"
  }
}

5 个答案:

答案 0 :(得分:34)

您需要使用HttpClient而不是HttpClientModule来发出http请求。

app.module.ts 中,导入HttpClientModule(请参阅下面的“导入”):

import { HttpClientModule } from '@angular/common/http';
@NgModule({
    imports: [
          HttpClientModule,
    ...
    ],
    ...
})
export class AppModule { }

然后,在 app.component.ts 中导入HttpClient

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

现在,从

更改AppComponent Ctor
constructor(private http: HttpClientModule){};

constructor(private http: HttpClient){};

答案 1 :(得分:9)

如果您不注意HTTPClient的导入,则会收到此错误。通常它默认为

import { HttpClient } from 'selenium-webdriver/http';

答案 2 :(得分:1)

从标题和导入中更改/添加这些代码/方法

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

并在导入部分添加此代码:

 HttpClientModule,

在图片中指出:

Pic1

在您尝试访问httpClient的类中包含这些标头:

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

并且对于地图,请使用此代码.map(res => res );代替.map((res: Response) => res.json() );

从这张照片中你可以看到如何解决' get'问题:

Pic2

获得清晰的概念观看pic1和pic2。 谢谢。

答案 3 :(得分:0)

app.module.ts

import { HttpClientModule } from '@angular/common/http'

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    HttpClientModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.ts

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

@Component ({
  selector:'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent {
  results;
  constructor(private http: HttpClient) {}

  ngOnInit() {
    this.todaydate = this.myservice.getTodayDate();
    this.http.get("http://jsonplaceholder.typicode.com/users").subscribe(data => {
      console.log(data);
     this.results = data;
   });
  }
}

答案 4 :(得分:0)

请按照以下说明检查您的文件,我的环境参数:

Angular CLI: 9.1.7
Node: 12.7.0
OS: darwin x64

Angular:
...
Ivy Workspace:

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.901.7
@angular-devkit/core         9.1.7
@angular-devkit/schematics   9.1.7
@schematics/angular          9.1.7
@schematics/update           0.901.7
rxjs                         6.5.4

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { CribListingComponent } from './crib-listing/crib-listing.component';
import { CribCardComponent } from './crib-card/crib-card.component';
import { HttpClientModule } from '@angular/common/http';


@NgModule({
  declarations: [
    AppComponent,
    CribListingComponent,
    CribCardComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.ts:

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'hello-world';
}

crib-listing.component.ts

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import 'rxjs/add/operator/map';

@Component({
  selector: 'app-crib-listing',
  templateUrl: './crib-listing.component.html',
  styleUrls: ['./crib-listing.component.css']
})
export class CribListingComponent implements OnInit {

  cribs: Array<any>;
  constructor(private http: HttpClient) { }

  ngOnInit(): void {
    // make an http request
    this.http.get('data/cribs.json')
    .subscribe(
      data => console.log(data)
    );
  }

}