相关疑难解决方法(0)

Angular CLI 6:放置库依赖项的位置

我正在转换库(ng-app-state)以使用angular cli,现在v6支持库(yay!).

在一些代码中进行脚手架和复制之后,这是我的第一个问题:

我如何/在哪里添加第三方依赖项?

package.json,或到projects/ng-app-state/package.json

angular-cli angular-cli-v6

39
推荐指数
2
解决办法
2万
查看次数

未描述的 Angular NG0203 控制台消息

当我将 MatProgressSpinnerModule 加载到 app.module.ts 中时,我收到带有“stacktrace”的空 NG0203 错误:

    tQ https://localhost:44378/client/main.js?v=tGcUggioS9SQmuEwtHgBh_nWyK0V2FItGwwRHNtEq90:1
    re https://localhost:44378/client/main.js?v=tGcUggioS9SQmuEwtHgBh_nWyK0V2FItGwwRHNtEq90:1
    fac https://localhost:44378/client/main.js?v=tGcUggioS9SQmuEwtHgBh_nWyK0V2FItGwwRHNtEq90:1
    hydrate https://localhost:44378/client/main.js?v=tGcUggioS9SQmuEwtHgBh_nWyK0V2FItGwwRHNtEq90:1
    get https://localhost:44378/client/main.js?v=tGcUggioS9SQmuEwtHgBh_nWyK0V2FItGwwRHNtEq90:1
    _resolveInjectorDefTypes https://localhost:44378/client/main.js?v=tGcUggioS9SQmuEwtHgBh_nWyK0V2FItGwwRHNtEq90:1
    _resolveInjectorDefTypes https://localhost:44378/client/main.js?v=tGcUggioS9SQmuEwtHgBh_nWyK0V2FItGwwRHNtEq90:1
    PM https://localhost:44378/client/main.js?v=tGcUggioS9SQmuEwtHgBh_nWyK0V2FItGwwRHNtEq90:1
    create https://localhost:44378/client/main.js?v=tGcUggioS9SQmuEwtHgBh_nWyK0V2FItGwwRHNtEq90:1
    wA https://localhost:44378/client/main.js?v=tGcUggioS9SQmuEwtHgBh_nWyK0V2FItGwwRHNtEq90:1
    invoke https://localhost:44378/client/polyfills.js:1
    onInvoke https://localhost:44378/client/main.js?v=tGcUggioS9SQmuEwtHgBh_nWyK0V2FItGwwRHNtEq90:1
    invoke https://localhost:44378/client/polyfills.js:1
    run https://localhost:44378/client/polyfills.js:1
    run https://localhost:44378/client/main.js?v=tGcUggioS9SQmuEwtHgBh_nWyK0V2FItGwwRHNtEq90:1
    bootstrapModuleFactory https://localhost:44378/client/main.js?v=tGcUggioS9SQmuEwtHgBh_nWyK0V2FItGwwRHNtEq90:1
    wA https://localhost:44378/client/main.js?v=tGcUggioS9SQmuEwtHgBh_nWyK0V2FItGwwRHNtEq90:1
    invoke https://localhost:44378/client/polyfills.js:1
    run https://localhost:44378/client/polyfills.js:1
    F https://localhost:44378/client/polyfills.js:1
    invokeTask https://localhost:44378/client/polyfills.js:1
    runTask https://localhost:44378/client/polyfills.js:1
    L https://localhost:44378/client/polyfills.js:1
    promise callback*d https://localhost:44378/client/polyfills.js:1
    scheduleTask https://localhost:44378/client/polyfills.js:1
    scheduleTask https://localhost:44378/client/polyfills.js:1
    scheduleMicroTask https://localhost:44378/client/polyfills.js:1
    F https://localhost:44378/client/polyfills.js:1
    then https://localhost:44378/client/polyfills.js:1
    bootstrapModule https://localhost:44378/client/main.js?v=tGcUggioS9SQmuEwtHgBh_nWyK0V2FItGwwRHNtEq90:1
    636 https://localhost:44378/client/main.js?v=tGcUggioS9SQmuEwtHgBh_nWyK0V2FItGwwRHNtEq90:1
    n https://localhost:44378/client/runtime.js:1
    <anonymous> https://localhost:44378/client/main.js?v=tGcUggioS9SQmuEwtHgBh_nWyK0V2FItGwwRHNtEq90:1
    a https://localhost:44378/client/runtime.js:1
    <anonymous> https://localhost:44378/client/main.js?v=tGcUggioS9SQmuEwtHgBh_nWyK0V2FItGwwRHNtEq90:1

Run Code Online (Sandbox Code Playgroud)

应用程序模块.ts:

import …
Run Code Online (Sandbox Code Playgroud)

angular-cli angular

6
推荐指数
1
解决办法
5760
查看次数

使用模块联合架构导入角度应用程序时,“必须从注入上下文调用inject()”

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

我尝试了这个答案中提供的解决方案,必须从注入上下文调用inject(),但它不起作用。

目前我在这两个应用程序中都使用 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": …
Run Code Online (Sandbox Code Playgroud)

angular micro-frontend webpack-5 angular11 webpack-module-federation

5
推荐指数
1
解决办法
4265
查看次数

升级到 Angular 11 后,必须从注入上下文中调用“inject()”

升级到 Angular 11 后,我无法再访问ng serve我的 Web 应用程序。

我正在使用 Spring Doc 和最新的 OpenAPI 生成器 gradle-plutin (5.0.0) 生成客户端。

该问题似乎与我的(生成的)REST 客户端有关。打开 https://localhost:4200 会将以下内容写入控制台:

main.ts:12 Error: inject() must be called from an injection context
    at injectInjectorOnly (core.js:4901) [angular]
    at Module.??inject (core.js:4911) [angular]
    at Object.ApiModule_Factory [as factory] (meditation-rest-client.js:2885) [angular]
    at R3Injector.hydrate (core.js:11158) [angular]
    at R3Injector.get (core.js:10979) [angular]
    at :4200/vendor.js:82591:55 [angular]
    at Set.forEach (<anonymous>) [angular]
    at R3Injector._resolveInjectorDefTypes (core.js:11016) [angular]
    at new NgModuleRef$1 (core.js:25046) [angular]
    at NgModuleFactory$1.create (core.js:25100) [angular]
    at :4200/vendor.js:100468:45 [angular]
    at Object.onInvoke (core.js:28301) …
Run Code Online (Sandbox Code Playgroud)

openapi angular openapi-generator springdoc

5
推荐指数
2
解决办法
1570
查看次数

Angular 升级 - 错误:必须从injectInjectorOnly 的注入上下文调用inject()

我有一个有角度的项目,结构如下:

\n

站点1

\n
    \n
  • 模块1
  • \n
  • 模块2
  • \n
  • SharedModule <- 不在库中,只是一个模块
  • \n
\n

站点2

\n
    \n
  • 模块3
  • \n
  • 模块4
  • \n
  • SharedModule <- 直接调用Main中的模块
  • \n
\n

我知道这并不理想,但事实就是如此。

\n

除了应用程序的名称之外,这些package.json文件完全相同。

\n

该项目是在 Angular 8 上启动的,并且一直升级得很好,但是从 11 到 12 我遇到了问题:

\n

Site1 构建良好并且运行良好。Site2 构建良好,但运行时立即出错

\n
\n

错误:必须从注入上下文\natjectInjectorOnly(核心:4745)\nat模块调用inject()。\xc9\xb5\xc9\xb5inject(核心:4755)\natObject.RouterModule_Factory [作为工厂](路由器:5999) )\nat R3Injector. Hydro (core.js:11438)\nat R3Injector.get (core.js:11257)\nat core.js:11295\nat Set.forEach ()\nat R3Injector._resolveInjectorDefTypes (core.js:11295 )\nat 新 NgModuleRef$1 (core.js:25325)\nat NgModuleFactory$1.create (core.js:25379)

\n
\n

由于 Site1 工作正常,我认为它一定是调用 SharedModule 的原因?

\n

我尝试添加"preserveSymlinks": true,到projects.$name.architect.build.options,但这不起作用。我没有使用npm link,因为另一个模块不是库(我知道它应该这样设置,但事实并非如此)。 …

angular

5
推荐指数
2
解决办法
8254
查看次数

如何在父类中注入服务而不将其传递到构造函数中?角6

我想知道如何在抽象类中注入具有多个依赖项的服务,该抽象类将由多个类扩展。

以更有效的方式将其传递到所有构造函数中!

我尝试创建静态,但如果该服务从未被另一个服务实例化,则永远不会分配单例实例变量

像这样的:(只是一个例子)

@Injectable({
  providedIn: 'root'
})
export class AnimalService {

  constructor(private http: HttpClient, private userService: UserService) {}

  countTotalInDB(type): number {
    return this.http.get(...);
  }

  getUserAnimals(userId: number) {
    return this.userService.getUser(userId).animals;
  }

}

abstract class Animal {

  constructor() {}

  public getTotalInDataBase(type): number {
    // How to get a instance of AnimalService ?
    return animalService.countTotalInDB(type);
  }

}

export class Cat extends Animal {

  constructor() {
    super();
  }

  public getTotalInDataBase(): number {
    return super.getTotalInDataBase('cat');
  }

}

export class Dog extends Animal {

  constructor() …
Run Code Online (Sandbox Code Playgroud)

dependency-injection typescript angular

3
推荐指数
1
解决办法
1899
查看次数