NullInjectorError:没有 InjectionToken DocumentToken 的提供者

2024-02-24

我正在为所有 Angular 5 项目设置一个通用库。库是 GitHub 存储库的克隆角度库入门套件 https://github.com/zurfyx/angular-library-starter-kit.

一切正常,直到我尝试使用HttpClientModule(后继者HttpModule).

以下是我的内容library.module.ts file:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HttpModule } from '@angular/http';
import { HttpClientModule } from '@angular/common/http';
import { MyService } from './my-service';

export * from './my-service';

@NgModule({
  imports: [
    CommonModule,
    HttpModule,
    HttpClientModule
  ],
  exports: [
  ],
  declarations: [
  ],
  providers: [
      MyService
  ],
})
export class LibraryModule { }

这是以下内容my-service.ts(这是唯一的文件在图书馆):

import { Injectable } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { Observable } from "rxjs/Observable";

@Injectable()
export class MyService {

    constructor(private httpClient: HttpClient) {
        //
    }

    getData(): Observable<any> {
        return this.httpClient.get<any>('test.json');
    }

}

该库构建时没有任何错误,但是当我尝试在新的 Angular 5 应用程序中使用它时(ng new myApp)它给出了一个错误:

AppComponent_Host.ngfactory.js? [sm]:1 ERROR Error: StaticInjectorError(AppModule)[HttpXsrfTokenExtractor -> InjectionToken DocumentToken]: 
  StaticInjectorError(Platform: core)[HttpXsrfTokenExtractor -> InjectionToken DocumentToken]: 
    NullInjectorError: No provider for InjectionToken DocumentToken!
    at _NullInjector.get (core.js:994)
    at resolveToken (core.js:1292)
    at tryResolveToken (core.js:1234)
    at StaticInjector.get (core.js:1102)
    at resolveToken (core.js:1292)
    at tryResolveToken (core.js:1234)
    at StaticInjector.get (core.js:1102)
    at resolveNgModuleDep (core.js:10847)
    at _createClass (core.js:10888)
    at _createProviderInstance$1 (core.js:10858)

我什至尝试过导入模块HttpModule and HttpClientModule在 Angular 5 应用程序中,但我遇到了同样的错误。

我也尝试过删除HttpModule来自应用程序和库,但仍然是相同的错误。

我同时使用两者的原因HttpModule and HttpClientModule是因为显然 https://github.com/angular/angular/issues/20101#issuecomment-341404350 HttpClientModule仍然取决于HttpModule去工作。我了解到这不是真的。

我该如何解决这个问题?

Note:新鲜的应用程序运行得很好ng serve直到我添加LibraryModule to its imports.


我读得不好GitHub 问题 https://github.com/angular/angular/issues/20101。显然,在使用本地图书馆时,您必须添加--preserve-symlinks论证ng serve.

ng serve --preserve-symlinks

而且也不需要导入HttpModule任何地方,无需导入HttpClientModule如果只有库依赖于它,则在应用程序内部。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

NullInjectorError:没有 InjectionToken DocumentToken 的提供者 的相关文章

随机推荐