【问题标题】:NullInjectorError: No provider for ConnectionBackendNullInjectorError:没有 ConnectionBackend 的提供者
【发布时间】:2019-11-08 08:52:05
【问题描述】:

我在尝试加载页面时收到以下错误。我已经尝试了 StackOverflow 上其他文章中的一些建议,但我无法找到解决方案。我的代码位于错误堆栈下方。

core.js:7187 ERROR Error: Uncaught (in promise): NullInjectorError: StaticInjectorError(AppModule)[Http -> ConnectionBackend]: 
  StaticInjectorError(Platform: core)[Http -> ConnectionBackend]: 
    NullInjectorError: No provider for ConnectionBackend!
NullInjectorError: StaticInjectorError(AppModule)[Http -> ConnectionBackend]: 
  StaticInjectorError(Platform: core)[Http -> ConnectionBackend]: 
    NullInjectorError: No provider for ConnectionBackend!
    at NullInjector.get (core.js:1354)
    at resolveToken (core.js:1681)
    at tryResolveToken (core.js:1607)
    at StaticInjector.get (core.js:1470)
    at resolveToken (core.js:1681)
    at tryResolveToken (core.js:1607)
    at StaticInjector.get (core.js:1470)
    at resolveNgModuleDep (core.js:23104)
    at NgModuleRef_.get (core.js:24192)
    at resolveDep (core.js:24722)
    at resolvePromise (zone-evergreen.js:797)
    at resolvePromise (zone-evergreen.js:754)
    at zone-evergreen.js:858
    at ZoneDelegate.invokeTask (zone-evergreen.js:391)
    at Object.onInvokeTask (core.js:30873)
    at ZoneDelegate.invokeTask (zone-evergreen.js:390)
    at Zone.runTask (zone-evergreen.js:168)
    at drainMicroTaskQueue (zone-evergreen.js:559)
    at ZoneTask.invokeTask [as invoke] (zone-evergreen.js:469)
    at invokeTask (zone-evergreen.js:1603)

app.module.ts:

我已经在 httpModule 的 import 中添加了,并将其添加到了 imports 数组中。

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

import { AppComponent } from './app.component';
import { NavMenuComponent } from './nav-menu/nav-menu.component';
import { HomeComponent } from './home/home.component';
import { ApiAuthorizationModule } from 'src/api-authorization/api-authorization.module';
import { AuthorizeGuard } from 'src/api-authorization/authorize.guard';
import { AuthorizeInterceptor } from 'src/api-authorization/authorize.interceptor';
import { MemberComponent } from './member/member.component';
import { HttpModule } from '@angular/http';

@NgModule({
  declarations: [
    AppComponent,
    NavMenuComponent,
    HomeComponent,
    MemberComponent,
  ],
  imports: [
    BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }),
    HttpModule,
    HttpClientModule,
    JsonpModule,
    FormsModule,
    ApiAuthorizationModule,
    RouterModule.forRoot([
      { path: '', component: HomeComponent, pathMatch: 'full' },
      { path: 'member', component: MemberComponent, canActivate: [AuthorizeGuard] },
    ])
  ],
  providers: [
    { provide: HTTP_INTERCEPTORS, useClass: AuthorizeInterceptor, multi: true }
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

这是页面模块 - member.component.ts:

我确切地知道哪一行代码触发了异常。它是构造函数。如果我注释掉构造函数,页面将加载。

import { Component } from '@angular/core';
import { Injectable } from '@angular/core';
import { HttpModule } from '@angular/http';
import { Http, Response } from '@angular/http';
import { Portfolio } from '../models/Portfolio';
import 'rxjs/add/operator/map';

@Component({
  selector: 'app-member-component',
  templateUrl: './member.component.html',
  providers: [Http, HttpModule]
})

@Injectable()
export class MemberComponent {

    constructor(private http: Http) { }

    public getPortfolios() {

        //return this.http.get('api/portfolio').map((r: Response) => <Portfolio><unknown>[] > r.json().data);
    }
}

你能发现我错过了什么吗?感谢您的帮助!

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    您似乎正在使用相对较旧的 Angular 版本。所以我的第一个建议是更新你的代码库。为了安全、速度和“轻松找到问题的答案”。

    第二点。您在AppModule 中导入HttpModuleHttpClientModuleHttpModule 已弃用,您不应再使用它。尽管如此,您也不应该同时导入两者,因此您需要从您的AppModule 中删除HttpModule

    另一个问题是JsonpModule,它也已被弃用,它的用例现在嵌入在HttpClient 中,因此您也应该删除此模块。

    之后,您的MemberComponent。您正在注入 Http。这是旧的已弃用 HttpModule 的服务。您应该将其替换为HttpClient。在使用HttpClient 时,您不必再使用.json(),因为这是自动完成的。

    此外,您不应该将 Http 提供程序添加到 @Component 定义中,并且您应该 - 永远 - 在提供程序数组中导入模块。

    最后一点,组件上的@Injectable 是多余的,因为所有组件都是可注入的。这给我们带来了这样的结果:

    @NgModule({
      declarations: [
        AppComponent,
        NavMenuComponent,
        HomeComponent,
        MemberComponent,
      ],
      imports: [
        BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }),
        HttpClientModule,
        FormsModule,
        ApiAuthorizationModule,
        RouterModule.forRoot([
          { path: '', component: HomeComponent, pathMatch: 'full' },
          { path: 'member', component: MemberComponent, canActivate: [AuthorizeGuard] },
        ])
      ],
      providers: [
        { provide: HTTP_INTERCEPTORS, useClass: AuthorizeInterceptor, multi: true }
      ],
      bootstrap: [AppComponent]
    })
    export class AppModule { }
    

    使用您的组件:

    @Component({
      selector: 'app-member-component',
      templateUrl: './member.component.html'
    })
    export class MemberComponent {
    
        constructor(private http: HttpClient) { }
    
        public getPortfolios() {
          return this.http.get<Portfolio[]>('api/portfolio');
        }
    }
    
    

    【讨论】:

    • 嗨皮埃尔。听到我正在使用旧版本,我感到很惊讶。不过,我是 Angular 的新手,也许我只是做了一些根本错误的事情。我正在使用 Visual Studio 2019,这是通过选择创建 Angular Web 应用程序生成的模板项目。我原以为这会给我一个相对较晚的 Angular 版本,但也许不是?如何升级到最新(稳定)版本的 Angular?谢谢!
    • @YossiGeretz 如果您也使用 angular cli,则可以运行 ng update @angular/cling update @angular/core。你运行的是什么版本?
    • angular/common, angular/core, angular/http, angular/platform-b​​rowser: (7.2.15); rxjs,rxjs-compat(6.5.3); zone.js (0.8.29)。 (不确定最后一个是什么,但它与其他文件夹一起列在 npm 文件夹下。)我对这些已经过时了吗?
    • 感谢皮埃尔的建议。正如 htn 指出的那样 - HttpClient 是从 @angular/common/http 导入的,而不是从 @angular/http 导入的。借助您的清理说明和 htn 的最终详细信息,我能够使其正常工作。我的 Angular 版本需要升级还是我只是使用过时的语法?
    • 最新的 Angular 版本是 8 和 9,但版本 7 差别不大。我看到你也在使用 rxjs-compat 模块,我也注意到你是如何构建你的 observables 的。我建议您删除此软件包,并查看指南 here 以了解如何升级您的代码库。
    【解决方案2】:

    你应该从@angular/common/http注入HttpClient

    【讨论】:

    • 我在哪里做这个?这里? constructor(private httpClient: HttpClient) { } 我试过这个。它从 'selenium-webdriver/http' 生成了 import { HttpClient };我不知道那是什么,现在 Web 应用程序不会以 404 开头。启动时:Microsoft.AspNetCore.SpaServices: Error: ERROR in ./node_modules/selenium-webdriver/http/index.js Module not found:错误:无法解析 'C:\Users***\node_modules\selenium-webdriver\http' 中的 'http' ./node_modules/selenium-webdriver/http/index.js 中的错误 找不到模块:错误:Can' t 解析 'C:\Users***\node_modules\selenium-webdriver\http' 中的 'https'
    • htn - 非常感谢您的帮助!我希望我能给你们两个答案打勾,因为你们都给了我足够的信息,当我把它放在一起时,我就能让它发挥作用。
    猜你喜欢
    • 1970-01-01
    • 2019-11-16
    • 2021-01-20
    • 2019-06-25
    • 2018-09-19
    • 2018-05-02
    • 2020-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多