NullInjectorError:否InjectionToken DocumentToken提供程序 [英] NullInjectorError: No provider for InjectionToken DocumentToken

查看:102
本文介绍了NullInjectorError:否InjectionToken DocumentToken提供程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm settings up a common library for all my Angular 5 projects. Library is a clone of GitHub repository angular-library-starter-kit.

一切正常,直到我尝试使用HttpClientModule(HttpModule的后继产品)为止.

Everything works fine until I try to use HttpClientModule (successor to HttpModule).

这是我的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的内容(库中的唯一文件):

And here are contents of my-service.ts (which is THE ONLY FILE in the library):

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)中使用它时,它给出了一个错误:

The library builds without any errors but when I try to use it in fresh Angular 5 app (ng new myApp) it gives an error:

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,但遇到相同的错误.

I've even tried importing modules HttpModule and HttpClientModule in the Angular 5 app but I got the same error.

我还尝试从应用程序和库中删除HttpModule,但仍然是相同的错误.

I've also tried removing HttpModule from both the app and library but still the same error.

我同时使用HttpModuleHttpClientModule的原因是因为

The reason I'm using both HttpModule and HttpClientModule is because apparently HttpClientModule still depends on HttpModule to work. I learned that this is not true.

我该如何解决?

注意:直到我将LibraryModule添加到imports之前,新的应用程序在ng serve上都可以正常工作.

Note: The fresh app works just fine with ng serve until I add LibraryModule to its imports.

推荐答案

我很少阅读 GitHub问题.显然,在使用本地库时,必须将--preserve-symlinks参数添加到ng serve.

I poorly read the GitHub Issue. Apparently when working with local libraries you have to add --preserve-symlinks argument to ng serve.

ng serve --preserve-symlinks

如果只依赖库,则无需在任何地方导入HttpModule,也无需导入HttpClientModule.

Also there is no need to import HttpModule anywhere and no need to import HttpClientModule inside the app if only the library depends on it.

这篇关于NullInjectorError:否InjectionToken DocumentToken提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆