简体   繁体   English

Angular HttpClientModule错误

[英]Angular HttpClientModule error

I am new to Angular, was trying the service http. 我是Angular的新手,正在尝试服务http。 But I came across this problem. 但是我遇到了这个问题。 Do you guys know what has gone wrong? 你们知道出什么问题了吗? Thanks 谢谢

在此处输入图片说明

from the terminal, the error stated is 从终端,指出的错误是

在此处输入图片说明

According to the error message, I went to module.d.ts file 根据错误信息,我去了module.d.ts文件

在此处输入图片说明



Thanks to you guys, i have changed it to HttpClient and the redline disappear, but I still got the error from terminal. 谢谢大家,我将其更改为HttpClient,并且红线消失了,但是仍然从终端收到错误消息。 and the page is still not showing. 并且页面仍未显示。 在此处输入图片说明

在此处输入图片说明



For anyone came across the error message in terminal, the HttpClientModule class in module.d.ts should be empty. 对于任何在终端中遇到错误消息的人,module.d.ts中的HttpClientModule类应该为空。 I added it accidentally throught code suggestion by typescript. 我是通过打字稿通过代码建议意外添加的。 Thanks for all the amazing helps 感谢所有的惊人帮助 在此处输入图片说明

import HttpClient 导入HttpClient

In app.module.ts 在app.module.ts中

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

imports:[HttpClientModule]

component.ts / service.ts component.ts / service.ts

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

constructor(private http:HttpClient){

}

It should be HttpClient . 应该是HttpClient That's what's used to make the AJAX Calls, 这就是进行AJAX调用的方式,

To use HttpClient though, you'll have to add the HttpClientModule to your imports array of the NgModule 但是,要使用HttpClient ,您必须将HttpClientModule添加到NgModule imports数组中

So it should be in your service: 所以应该为您服务:

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

export class ProductService {
  private productUrl = 'api/products/products.json';

  constructor(private http: HttpClient) {}

  getProducts() {
    ...
  }
}

And this in your Module: 这在您的模块中:

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

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

@NgModule({
  imports:      [ BrowserModule, FormsModule, HttpClientModule ],
  declarations: [ /*Your Components Here*/ ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

Here's a Stackblitz Project to help you out with it. 这是一个Stackblitz项目可以帮助您解决这个问题。

The issue is that you are using the HttpClientModule inside your service file, you should be using the HttpClient class instead. 问题是,你正在使用的HttpClientModule你的服务文件中,你应该使用HttpClient类来代替。 The HttpClientModule should be imported into your app.module.ts file, see below: HttpClientModule应该导入到您的app.module.ts文件中,如下所示:

app.module.ts

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

@NgModule({
  imports: [..., HttpClientModule, ..],
  ...
})

*.service.ts

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

...

constructor(public http: HttpClient) { }

getProducts(): Observable<IProduct[]> {
  const productUrl: '...';

  this.http.get<IProduct[]>(productUrl).pipe(
    tap(...),
    catchError(...)
  );
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM