我下面这个关于HTTP上的角基础教程。
在“设置:安装模块”部分中可以看到,他们按如下所示导入HttpClientModule:
import {HttpClientModule} from '@angular/common/http';
当我在项目中尝试此操作时,出现以下错误:“找不到模块'@ angular / common / http'”。
我尝试导入以下模块,如下所示:
import { HttpModule } from '@angular/http';
然后是我的导入部分:
imports: [
HttpModule
],
现在的问题是,我无法将此HttpModule注入我的服务对象,并且出现以下错误:“找不到模块HttpModule”。
这是我的服务班级:
import { Injectable, OnInit } from '@angular/core';
//Custom Models
import { Feed } from '../Models/Feed';
@Injectable()
export class FeedsService {
constructor(private httpClient: HttpModule) {}
}
我究竟做错了什么?
更新当我意识到我无法按照教程导入模块时,我应该做的就是运行npm update
命令,更新所有软件包。
重要提示:HttpClientModule
适用于Angular 4.3.0及更高版本。查看@Maximus的评论和@Pengyy的 答案以获取更多信息。
您需要注入HttpClient
组件/服务而不是模块。如果您导入HttpClientModule
您的@NgModule
// app.module.ts:
import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
// Import HttpClientModule from @angular/common/http
import {HttpClientModule} from '@angular/common/http';
@NgModule({
imports: [
BrowserModule,
// Include it under 'imports' in your application module
// after BrowserModule.
HttpClientModule,
],
})
export class MyAppModule {}
所以改变
constructor(private httpClient: HttpModule) {}
至
constructor(private httpClient: HttpClient) {}
就像在文档中所写的那样:https : //angular.io/guide/http#making-a-request-for-json-data
但是,由于您导入了HttpModule
你需要注入constructor(private httpClient: Http)
作为@Maximus评价和陈述@Pengyy在这个答案。
以及有关HttpModule
和之间的差异的更多信息HttpClientModule
,请查看以下答案:https : //stackoverflow.com/a/45129865/5706293
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句