【问题标题】:Angular 9 SSR build throws error for NgLocaleLocalizationAngular 9 SSR 构建为 NgLocaleLocalization 引发错误
【发布时间】:2020-08-03 06:59:07
【问题描述】:

所以我在 firebase 中托管了一个复杂的 Angular 项目。到目前为止,我正在做一个普通的客户端渲染,最近我转向 SSR 并将我的项目升级到 Angular 9 版本。当我运行ng build --prod --aot && ng run myproject:server:production 和运行node dist/server/main 时,当我尝试访问localhost:port 时会抛出以下错误。

Error: Can't resolve all parameters for NgLocaleLocalization: (?).
    at getUndecoratedInjectableFactory (\MyProject\dist\server\main.js:1:4440360)     
    at injectableDefOrInjectorDefFactory (\MyProject\dist\server\main.js:1:4441186)   
    at providerToFactory (\MyProject\dist\server\main.js:1:4442452)
    at providerToRecord (\MyProject\dist\server\main.js:1:4438546)
    at R3Injector.processProvider (\MyProject\dist\server\main.js:1:4438606)
    at provider (\MyProject\dist\server\main.js:1:4438102)
    at input.forEach.value (\MyProject\dist\server\main.js:1:4326776)
    at Array.forEach ()
    at deepForEach (\MyProject\dist\server\main.js:1:4326718)
    at R3Injector.processInjectorType (\MyProject\dist\server\main.js:1:4438062)

但是当我运行ng serve 时,它运行良好,没有任何问题。所以我怀疑问题出在 SSR 构建上。直到我切换到 Angular 9 才出现。Ivy 也可能是原因。但据我阅读 angular 的官方网站,我从未在我的应用程序的任何地方从@angular/common 扩展NgLocaleLocalization 类。

我还尝试在app.module 中提供NgLocalization 作为提供者,但即使这样也无济于事。 谷歌中没有人报告或提到过这个问题。我还在 github repo 中创建了一个问题,但直到现在还没有人回复。我完全没有选择。

package.json

"dependencies": {
    "@agm/core": "^1.1.0",
    "@agm/snazzy-info-window": "^1.1.0",
    "@angular/animations": "~9.1.2",
    "@angular/cdk": "^9.2.1",
    "@angular/common": "~9.1.2",
    "@angular/compiler": "~9.1.2",
    "@angular/core": "~9.1.2",
    "@angular/fire": "^5.4.2",
    "@angular/forms": "~9.1.2",
    "@angular/localize": "^9.1.2",
    "@angular/platform-browser": "~9.1.2",
    "@angular/platform-browser-dynamic": "~9.1.2",
    "@angular/platform-server": "~9.1.2",
    "@angular/router": "~9.1.2",
    "@ks89/angular-modal-gallery": "^7.2.5",
    "@ng-bootstrap/ng-bootstrap": "^6.0.2",
    "@nguniversal/express-engine": "^9.1.0",
    "@nguniversal/module-map-ngfactory-loader": "8.2.6",
    "angular-count-to": "0.0.3",
    "angular-tilt": "^1.2.0",
    "angular-typing-animation": "^0.5.4",
    "animate-scss": "^3.2.1",
    "animate.css": "^3.7.2",
    "bootstrap": "^4.4.1",
    "classlist.js": "^1.1.20150312",
    "cookieconsent": "^3.1.1",
    "core-js": "^3.6.5",
    "express": "^4.17.1",
    "firebase": "^7.14.1",
    "firebaseui": "4.5.0",
    "firebaseui-angular": "^4.1.0",
    "hammerjs": "^2.0.8",
    "jspdf": "^1.5.3",
    "jspdf-autotable": "^3.4.3",
    "lodash": "4.17.15",
    "moment-timezone": "^0.5.28",
    "mousetrap": "^1.6.5",
    "ng-animate": "^0.3.4",
    "ng2-animate-on-scroll": "^2.0.0",
    "ng2-cookies": "^1.0.12",
    "ng2-scroll-to-el": "^1.2.1",
    "ng5-slider": "^1.2.4",
    "ngx-build-plus": "^9.0.6",
    "ngx-content-loading": "^0.1.3",
    "ngx-cookieconsent": "^2.2.3",
    "ngx-logger": "^4.1.5",
    "ngx-masonry": "^9.1.1",
    "ngx-masonry-gallery": "^3.0.0",
    "ngx-owl-carousel-o": "^3.0.0",
    "ngx-spinner": "^9.0.2",
    "ngx-swiper-wrapper": "^9.0.1",
    "ngx-toastr": "^12.0.1",
    "razorpay": "^2.0.6",
    "rxjs": "^6.5.5",
    "rxjs-compat": "^6.5.5",
    "snazzy-info-window": "^1.1.1",
    "sweetalert2": "^9.10.12",
    "tslib": "^1.11.1",
    "web-animations-js": "^2.3.2",
    "zone.js": "~0.10.3"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.901.1",
    "@angular/cli": "^9.1.1",
    "@angular/compiler-cli": "~9.1.2",
    "@angular/language-service": "~9.1.2",
    "@types/express-serve-static-core": "^4.17.5",
    "@types/googlemaps": "^3.39.3",
    "@types/hammerjs": "^2.0.36",
    "@types/jasmine": "~3.5.10",
    "@types/jasminewd2": "~2.0.8",
    "@types/lodash": "^4.14.149",
    "@types/mousetrap": "^1.6.3",
    "@types/node": "^13.11.1",
    "bufferutil": "^4.0.1",
    "codelyzer": "^5.2.2",
    "jasmine-core": "~3.5.0",
    "jasmine-spec-reporter": "~5.0.1",
    "karma": "~5.0.2",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~2.1.1",
    "karma-jasmine": "~3.1.1",
    "karma-jasmine-html-reporter": "^1.5.3",
    "localstorage-polyfill": "^1.0.1",
    "protractor": "~5.4.4",
    "replace-in-file": "^5.0.2",
    "serverless-webpack": "^5.3.1",
    "ts-loader": "^7.0.0",
    "ts-node": "~8.8.2",
    "tslint": "~6.1.1",
    "typescript": "^3.8.3",
    "utf-8-validate": "^5.0.2",
    "webpack-cli": "^3.3.11",
    "ws": "^7.2.3",
    "xhr2": "^0.2.0"
  }

知道这个错误到底在说什么吗?如何解决这个问题或者我还有什么要尝试的? :)

任何建议/建议/解决方案都非常感谢。


更新 - TBH,我没有在我的应用程序中使用本地化,因此我从问题出现的地方继续调试,然后我发现我使用 ng-bootstrap 需要 @angular/localize要包括的包。但即使在这之后,我也无法得到解决方案。

【问题讨论】:

  • 您找到解决问题的方法了吗?
  • @DineshRawat 我想我刚刚更新了所有依赖项,这很可能解决了这个问题.. :)
  • 我会试一试,我正在尝试找到导致此问题的确切模块。对我来说可能是羽毛笔编辑器
  • 是的..很难调查根本原因..

标签: angular ivy ng-bootstrap angular-universal angular9


【解决方案1】:

如果您查看the source code 中的NgLocaleLocalization,您会发现它需要LOCALE_ID 令牌

@Injectable()
export class NgLocaleLocalization extends NgLocalization {
  constructor(@Inject(LOCALE_ID) protected locale: string) {
    super();
  }

也许尝试在你的代码中提供这个

providers: [{provide: LOCALE_ID, useValue: 'en-US' }] //Or any other language

如果可行,则根据需要提供基于工厂的语言环境

【讨论】:

  • 好吧,我确实尝试在app.module.ts 中添加这个,但不幸的是,没有运气。仍然抛出错误.. :(
  • 你找到解决办法了吗?
猜你喜欢
  • 2020-06-07
  • 2020-10-16
  • 2020-10-07
  • 2020-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-08
  • 1970-01-01
相关资源
最近更新 更多