gpt4 book ai didi

angular - 类型 'get' 上不存在属性 'HttpClientModule'

转载 作者:太空狗 更新时间:2023-10-29 16:57:41 26 4
gpt4 key购买 nike


我是 Angular js 的新手,试图从 youtube 学习它,但我在我的代码中遇到了提到的问题
,我不明白我的代码中缺少什么

错误

当我尝试访问 HttpClientModule 的获取服务时出现“属性‘get’在类型‘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"
}
}

最佳答案

您需要使用 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){};

关于angular - 类型 'get' 上不存在属性 'HttpClientModule',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47369850/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com