Angular 5 Upgrade NullInjectorError: No provider for InjectionToken LocaleId

纵然是瞬间 提交于 2020-05-14 07:44:08

问题


I have an Angular 4 app that I have upgraded to Angular 5 with CLI 1.5.4. I also have a shared library using npm link. After upgrading I had to add this to my tsconfig.app.json to get the CLI building again:

  "include": [
    "./**/*.ts",
    "../node_modules/my-lib/src/**/*.ts",
    "../node_modules/my-lib/index.ts",
    "../node_modules/my-lib/ng2-icad-cf.ts"
  ],

After a successful build, I am presented with the following error when visiting localhost:4200

NullInjectorError: No provider for InjectionToken LocaleId!

I could not find any references to LOCALE / LocaleId in my project. I suspect the linked library might be the issue as I had the same NullIjectorError for Http (library uses Http, project uses HttpClient) but it has since disappeared after adding HttpClient to the library's main module, even after reverting back and removing it... very strange.


回答1:


I experienced the same issue.

First, make sure to use "peerDependencies" for any Angular libraries that your module depends on as described here: https://github.com/angular/angular-cli/issues/3854#issuecomment-274344771

Then try running / building the app by passing --preserve-symlinks to Angular CLI. For example:

ng serve --preserve-symlinks

See https://github.com/angular/angular-cli/issues/8677#issuecomment-347885068 for more details.

Also, this may help: https://github.com/angular/angular-cli/wiki/stories-linked-library




回答2:


Import LOCALE_ID module in your testing file and then export as GLOBAL_PROVIDER.

import { LOCALE_ID } from '@angular/core';


export const GLOBAL_PROVIDER = [
  { provide: LOCALE_ID, useValue: 'en'}
];


来源:https://stackoverflow.com/questions/47540108/angular-5-upgrade-nullinjectorerror-no-provider-for-injectiontoken-localeid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!