npm 找不到模块“@angular/common/http”

hjzp0vay  于 2023-06-23  发布在  Angular
关注(0)|答案(4)|浏览(128)

我已经把我的npm升级到了最新版本。我的Visual Studio应用程序中的npm文件夹读取v6.0.3,而我的package.json文件也对应于这个版本6.0.3。
但任何时候我运行我的应用程序我得到错误找不到模块在浏览器上

我相信HttpClient在3x以上的版本中使用,但显然安装了更高的版本。
app.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { RouterModule } from '@angular/router';

import { AppComponent } from './components/app/app.component';
import { NavMenuComponent } from './components/navmenu/navmenu.component';
import { HomeComponent } from './components/home/home.component';
import { FetchDataComponent } from './components/fetchdata/fetchdata.component';
import { CounterComponent } from './components/counter/counter.component';
import { QuizListComponent } from './components/quiz/quiz-list.component';


@NgModule({
    declarations: [
        AppComponent,
        NavMenuComponent,
        CounterComponent,
        FetchDataComponent,
        HomeComponent,
        QuizListComponent
    ],
    imports: [
        CommonModule,
        HttpClientModule,
        FormsModule,
        RouterModule.forRoot([
            { path: '', redirectTo: 'home', pathMatch: 'full' },
            { path: 'home', component: HomeComponent },
            { path: 'counter', component: CounterComponent },
            { path: 'fetch-data', component: FetchDataComponent },
            { path: '**', redirectTo: 'home' }
        ])
    ]
})
export class AppModuleShared {
}

pakage.json

{
  "name": "ExamBuddy",
  "private": true,
  "version": "0.0.0",
  "scripts": {
    "test": "karma start ClientApp/test/karma.conf.js"
  },
  "devDependencies": {
    "@angular/animations": "6.0.3",
    "@angular/common": "6.0.3",
    "@angular/compiler": "6.0.3",
    "@angular/compiler-cli": "6.0.3",
    "@angular/core": "6.0.3",
    "@angular/forms": "6.0.3",
    "@angular/http": "6.0.3",
    "@angular/platform-browser": "6.0.3",
    "@angular/platform-browser-dynamic": "6.0.3",
    "@angular/platform-server": "6.0.3",
    "@angular/router": "6.0.3",
    "@ngtools/webpack": "6.0.7",
    "@types/chai": "4.1.3",
    "@types/jasmine": "2.8.7",
    "@types/webpack-env": "1.13.6",
    "angular2-router-loader": "0.3.5",
    "angular2-template-loader": "0.6.2",
    "aspnet-prerendering": "^3.0.1",
    "aspnet-webpack": "^3.0.0",
    "awesome-typescript-loader": "5.0.0",
    "bootstrap": "4.1.1",
    "chai": "4.1.2",
    "css": "2.2.3",
    "css-loader": "0.28.11",
    "es6-shim": "0.35.3",
    "event-source-polyfill": "0.0.12",
    "expose-loader": "0.7.5",
    "extract-text-webpack-plugin": "3.0.2",
    "file-loader": "1.1.11",
    "html-loader": "0.5.5",
    "isomorphic-fetch": "2.2.1",
    "jasmine-core": "3.1.0",
    "jquery": "3.3.1",
    "json-loader": "0.5.7",
    "karma": "2.0.2",
    "karma-chai": "0.1.0",
    "karma-chrome-launcher": "2.2.0",
    "karma-cli": "1.0.1",
    "karma-jasmine": "1.1.2",
    "karma-webpack": "3.0.0",
    "preboot": "6.0.0-beta.4",
    "raw-loader": "0.5.1",
    "reflect-metadata": "0.1.12",
    "rxjs": "6.2.0",
    "style-loader": "0.21.0",
    "to-string-loader": "1.1.5",
    "typescript": "2.8.3",
    "url-loader": "1.0.1",
    "webpack": "4.10.2",
    "webpack-hot-middleware": "2.22.2",
    "webpack-merge": "4.1.2",
    "zone.js": "0.8.26"
  }
}
bf1o4zei

bf1o4zei1#

您需要拥有最新版本的angular http

npm install @angular/http@latest

在服务组件中导入HttpClient(如果需要):

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

app.module.ts中导入HttpClientModule//代码中已经存在:

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

y0u0uwnf2#

这个问题有一些解决方案,它们是:
1.如果你的项目有多个模块,那么你必须在每个模块中从'@angular/common/http'导入HttpClientModule
1.从项目的模块中添加HttpClient到提供程序中,请参见示例:

providers:[
   UserService, 
   HttpClient]

最好将HttpClientModule导入每个模块。
1.删除node_modules文件夹并清理npm缓存。然后输入npm i安装所有依赖项。执行以下代码:rm -rf node_modules/ && npm cache clean -f && npm i`

ghg1uchk

ghg1uchk3#

您可能会尝试import HttpMoudle from '@angular/common/http',因此在任何情况下,Http和HttpModule都可以从'@angular/http'导入,但HttpClient和HttpClientModule是从'@angular/common/http'导入的。
因此,如果在组件/服务中使用HttpClient且必须在@NgModule中导入HttpClientModule
示例:

// app.module.ts:

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

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

@NgModule({
  imports: [
    BrowserModule,
    // Include it under 'imports' in your application module
    // after BrowserModule.
    HttpClientModule,
  ],
})
export class MyAppModule {}
avkwfej4

avkwfej44#

尝试以下命令:

npm i @angular

相关问题