2

我正在尝试按照这个示例Module federation with angular 使用webpack5 模块联合以 Angular 实现微前端概念,但我遇到了这个错误错误:未捕获(在承诺中):错误:必须从注入上下文调用注入()

我尝试了这个答案中提供的解决方案inject() must be called from an injection context但它不起作用。

目前我在两个应用程序中都使用 Angular 版本 11。

正在导入的应用的 package.json(子)

    "resolutions": {
    "webpack": "5.0.0"
  },
  "dependencies": {
    "@angular/animations": "11.0.0-next.6",
    "@angular/cdk": "11.0.0-next.6",
    "@angular/common": "11.0.0-next.6",
    "@angular/compiler": "11.0.0-next.6",
    "@angular/core": "11.0.0-next.6",
    "@angular/forms": "11.0.0-next.6",
    "@angular/platform-browser": "11.0.0-next.6",
    "@angular/platform-browser-dynamic": "11.0.0-next.6",
    "@angular/platform-server": "^11.0.0",
    "@angular/router": "11.0.0-next.6",
    "@angular/service-worker": "11.0.0-next.6",
    "@fortawesome/angular-fontawesome": "^0.8.0",
    "@fortawesome/fontawesome-svg-core": "^1.2.32",
    "@fortawesome/free-solid-svg-icons": "^5.15.1",
    "@ngx-translate/core": "^13.0.0",
    "@ngx-translate/http-loader": "^6.0.0",
    "angular-font-awesome": "^3.1.2",
    "bootstrap": "^4.3.1",
    "classlist.js": "^1.1.20150312",
    "core-js": "^3.1.4",
    "file-saver": "^2.0.2",
    "font-awesome": "^4.7.0",
    "js2xmlparser": "^4.0.0",
    "moment": "^2.25.3",
    "moment-timezone": "^0.5.28",
    "ngx-webstorage-service": "^4.1.0",
    "node-sass": "^4.12.0",
    "post-robot": "7.0.15",
    "primeicons": "^1.0.0",
    "rxjs": "6.6.3",
    "rxjs-tslint": "^0.1.7",
    "zone.js": "^0.10.3",
    "@angular-architects/module-federation": "^0.9.3"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "0.1100.0-next.6",
    "@angular/cli": "~11.0.1",
    "@angular/compiler-cli": "11.0.0-next.6",
    "@angular/language-service": "^11.0.0",
    "@types/jasmine": "~3.6.0",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~12.6.8",
    "codelyzer": "^6.0.0",
    "jasmine-core": "~3.6.0",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "~5.0.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~3.0.2",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "protractor": "~7.0.0",
    "ts-node": "~8.3.0",
    "tslint": "~6.1.0",
    "typescript": "~4.0.5",
    "serve": "^11.3.2",
    "ngx-build-plus": "^10.1.1"
  }

应用程序的 package.json 导入另一个应用程序(父)

"resolutions": {
    "webpack": "5.0.0"
  },
  "dependencies": {
    "@angular-architects/module-federation": "^0.9.3",
    "@angular/animations": "^11.0.0",
    "@angular/cdk": "^11.0.0",
    "@angular/common": "11.0.0-next.6",
    "@angular/compiler": "11.0.0-next.6",
    "@angular/core": "11.0.0-next.6",
    "@angular/forms": "11.0.0-next.6",
    "@angular/platform-browser": "11.0.0-next.6",
    "@angular/platform-browser-dynamic": "11.0.0-next.6",
    "@angular/router": "11.0.0-next.6",
    "@ngx-translate/core": "^12.1.2",
    "@ngx-translate/http-loader": "^5.0.0",
    "bootstrap": "^4.5.2",
    "bowser": "^2.11.0",
    "classlist": "^2.0.0",
    "classlist.js": "^1.1.20150312",
    "core-js": "^3.6.5",
    "dc-central-modules": "^0.1.27",
    "moment-timezone": "^0.5.28",
    "post-robot": "7.0.15",
    "primeng": "^9.0.0-rc.1",
    "query-string": "^6.13.7",
    "rxjs": "~6.5.4",
    "tslib": "^2.0.0",
    "zone.js": "~0.10.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "0.1100.0-next.6",
    "@angular/cli": "~11.0.1",
    "@angular/compiler-cli": "11.0.0-next.6",
    "@types/jasmine": "~3.6.0",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^12.11.1",
    "codelyzer": "^6.0.0",
    "jasmine-core": "~3.6.0",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "~5.0.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~3.0.2",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "karma-phantomjs-launcher": "^1.0.4",
    "protractor": "~7.0.0",
    "serve": "^11.3.2",
    "ts-node": "~8.3.0",
    "tslint": "~6.1.0",
    "typescript": "~4.0.5",
    "ngx-build-plus": "^10.1.1"
  }
4

3 回答 3

0

就我而言,我使用的是requiredVersion: 'auto' and sharedMappings.getDescriptors()from @angular-architects/module-federationpackage。所以我指定了确切的版本。

"@angular/core": { singleton: false, strictVersion: true, requiredVersion: '12.2.15' },
"@angular/common": { singleton: false, strictVersion: true, requiredVersion: '12.2.15' },
"@angular/router": { singleton: false, strictVersion: true, requiredVersion: '12.2.15' },
于 2022-01-17T23:37:57.203 回答
0

当您的任何孩子依赖没有遇到角核心或角共同时,这可能会发生。尝试检查控制台/终端以获取更多信息。在我的情况下,例如:highchart 我已经安装在子组件中,并获得对 angular core 10.0.0 与父组件的对等依赖。一旦我更新了版本,错误就消失了。

于 2021-01-21T05:22:59.720 回答
0

面对同样的问题,在弄清楚@angular/core并且@angular/common(并且可能@ngx-translate/core)必须在两个 webpack 配置中使用“singleton: true”标志共享和加载一次之后,错误就解决了。

于 2021-01-20T13:59:09.623 回答