【问题标题】:Cannot find module "@angular/common/http"找不到模块“@angular/common/http”
【发布时间】:2018-11-10 08:29:21
【问题描述】:

我已将我的 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 {
}

包装.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"
  }
}

【问题讨论】:

  • 你可能会尝试从'@angular/common/http'导入httpCilent;
  • 从来没有一个版本3 angular,无论如何你可以在运行这个命令rm -rf node_modules/ && npm cache clean -f之后尝试
  • 我的意思是 Package.json 文件中的版本......

标签: angular npm package.json


【解决方案1】:

这个问题有一些解决方案,它们是:

  1. 如果您的项目有多个模块,那么您必须在每个模块中从'@angular/common/http' 导入HttpClientModule

  2. 在您项目的模块中的提供程序中添加 HttpClient,请参见示例:

    providers:[
       UserService, 
       HttClient]
    

但请记住,这不是编码的好方法,但它确实有效。

  1. 删除 node_modules 文件夹并清理 npm 缓存。然后安装所有依赖项,键入`npm i.执行下面的代码:

rm -rf node_modules/ && npm cache clean -f && npm i

【讨论】:

    【解决方案2】:

    你需要有最新版本的angular http:

    npm install @angular/http@latest
    

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

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

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

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

    【讨论】:

      【解决方案3】:

      试试下面的命令:

      npm i @angular
      

      【讨论】:

        【解决方案4】:

        你可以尝试从'@angular/common/http'导入HttpMoudle,所以无论如何 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 {}
        

        【讨论】:

        • 我添加了以下 import { HttpClientModule } from '@angular/common/http';到 app.module,我也将它包含在导入中。但我仍然有同样的问题。
        • 你试过运行 npm install 吗??
        • 是的,我有。 npm 安装正常
        • 你能和我们分享一下app.module.ts
        猜你喜欢
        • 2017-12-25
        • 1970-01-01
        • 2018-01-23
        • 1970-01-01
        • 1970-01-01
        • 2023-03-20
        • 1970-01-01
        • 2022-11-10
        • 2017-01-02
        相关资源
        最近更新 更多