【问题标题】:NullInjectorError: No provider for InjectionToken DocumentTokenNullInjectorError:没有 InjectionToken DocumentToken 的提供者
【发布时间】:2019-02-27 22:27:32
【问题描述】:

我正在为我的所有 Angular 5 项目设置一个通用库。库是 GitHub 存储库 angular-library-starter-kit 的克隆。

在我尝试使用 HttpClientModuleHttpModule 的继任者)之前一切正常。

这是我的library.module.ts 文件的内容:

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)

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

我也尝试从应用程序和库中删除 HttpModule,但仍然出现相同的错误。

我同时使用HttpModuleHttpClientModule 的原因是因为apparently HttpClientModule 仍然依赖HttpModule 才能工作。 我了解到这不是是的。

我该如何解决这个问题?

注意:新鲜的应用程序在 ng serve 上运行得很好,直到我将 LibraryModule 添加到它的 imports

【问题讨论】:

  • 为什么只从我的服务导入,然后立即导出?
  • @rrd 因为如果我不导出它,我会得到Module: 'library' has no exported member 'MyService'。你有什么建议吗?我希望我的库中公开的所有内容都可以从单个文件中导入。

标签: angular


【解决方案1】:

我对@9​​87654321@ 的阅读很差。显然,在使用本地库时,您必须将 --preserve-symlinks 参数添加到 ng serve

ng serve --preserve-symlinks

此外,如果只有库依赖,则无需在任何地方导入HttpModule,也无需在应用内导入HttpClientModule

【讨论】:

  • 谢谢!你救了我从金门大桥上跳下来。这应该由 Angular 更好地记录。
【解决方案2】:

更新到 Eric 的 答案:

由于 Angular/Cli v6 --preserve-symlinks 参数已被删除。要添加它,请相应地编辑 angular.json 文件:

[...]  
  "architect": {
    "build": {
      "builder": "@angular-devkit/build-angular:browser",
      "options": {
        "progress": true,
        "extractCss": false,
        "outputPath": "dist/xcs-web-ui",
        "index": "src/index.html",
        "main": "src/main.ts",
        "polyfills": "src/polyfills.ts",
        "tsConfig": "src/tsconfig.app.json",
        "preserveSymlinks": true,
[...]          

【讨论】:

    猜你喜欢
    • 2018-10-19
    • 2018-03-26
    • 2021-12-18
    • 2018-07-16
    • 1970-01-01
    • 2021-09-08
    • 2021-12-15
    • 2019-04-18
    • 2019-11-16
    相关资源
    最近更新 更多