Error: NG0203: inject() must be called from an injection context such as a constructor, a factory function, a field initializer, or a function used

huangapple go评论59阅读模式
英文:

Error: NG0203: inject() must be called from an injection context such as a constructor, a factory function, a field initializer, or a function used

问题

以下是已翻译的部分:

The setup:

  1. MongoDB (Atlas or local)
  2. Backend: NestJS, OAS, using swagger-codegen to package 'backend client' for...
  3. Angular 15 + Material Design + Ngrx (store + effects)

Recently I added the state manage work using Ngrx and when I add the EffectsModule I get this error:

Error: NG0203: inject() must be called from an injection context such as a constructor, a factory function, a field initializer, or a function used with EnvironmentInjector#runInContext.

Issue began when adding EffectsModule to import (line 36) of app.module.ts

I continue my research of this, and would appreciate any thoughts or insights. Based on this SO article (1) the error appears generic. I am open to ideas of how to chase this down.

Repo branches necessary to replicate environment:

  1. Angular client
  2. Backend API

Other SO articles I have come across:

  1. Angular upgrade - Error: inject() must be called from an injection context at injectInjectorOnly
  2. inject() must be called from an injection context when using angular library in ionic project

Causes error to change to: NullInjectorError: NullInjectorError: No provider for Store!

  1. Services generated by openapi-generator-cli not injectable

LATEST:

I suspected that the typescript being generated by the openapitools/openapi-generator-cli is no longer sufficient whilst including the ngrx work. I did find the typescript-angular generator in the list of generator and will give this a try...

英文:

The setup:

  1. MongoDB (Atlas or local)
  2. Backend: NestJS, OAS, using swagger-codegen to package 'backend client' for...
  3. Angular 15 + Material Design + Ngrx (store + effects)

Recently I added the state manage work using Ngrx and when I add the EffectsModule I get this error:

>Error: NG0203: inject() must be called from an injection context such as a constructor, a factory function, a field initializer, or a function used with EnvironmentInjector#runInContext.

Issue began when adding EffectsModule to import (line 36) of app.module.ts

I continue my research of this, and would appreciate any thoughts or insights. Based on this SO article (1) the error appears generic. I am open to ideas of how to chase this down.

Repo branches necessary to replicate environment:

  1. Angular client
  2. Backend API

Other SO articles I have come across:

  1. Angular upgrade - Error: inject() must be called from an injection context at injectInjectorOnly

  2. inject() must be called from an injection context when using angular library in ionic project
    > Causes error to change to: NullInjectorError: NullInjectorError: No provider for Store!

  3. Services generated by openapi-generator-cli not injectable

LATEST:

I suspected that the typescript being generated by the openapitools/openapi-generator-cli is no longer sufficient whilst including the ngrx work. I did find the typescript-angular generator in the list of generator and will give this a try...

答案1

得分: 2

问题可能有几个地方。具体来说,正确设置模块是其中之一。这篇文章在指出我没有正确设置和引用API包时很有帮助。

具体来说,在“Usage example”部分,代码如下:

import { PostsService } from './core/api/v1';

因此,我在运行openapi-generator时更改了输出参数的路径。我直接引用输出,而不是使用npm link

英文:

The issue was likely a few things. Specifically, setting up the module correctly. This article was useful in pointing out that I had not setup and referenced the API package correctly.

Specifically the part under heading "Usage example" where code shows like:

import { PostsService } from './core/api/v1';

Therefore, I change the path to my output arg when running the openapi-generator. Instead of using npm link I referenced the output directly.

答案2

得分: 0

我在Chrome控制台日志中遇到了相同的错误。我有一个我创建的Angular库,并将其导入到现有的Angular应用程序中。我使用npm install C:/...dist/myapp来安装该库。

我的应用程序在angular.json中缺失:
projects/myapp/architect/build/options/preserveSymlinks: true

将preserveSymlinks更改为true后重新安装,我不再遇到上述错误。

英文:

I had the same error in the Chrome console logs. I had an angular library I created and imported it into an existing angular app. I was using npm install C:/...dist/myapp to install the library.

My app was missing in the angular.json:
projects/myapp/architect/build/options/preserveSymlinks: true

After changing preserveSymlinks to true, reinstalling, I no longer had the mentioned error.

答案3

得分: 0

// 使用inject函数导入你的路由器:
const router = inject(Router);

英文:
// Import your router with inject function :
const router = inject(Router) ;

huangapple
  • 本文由 发表于 2023年2月17日 23:51:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75486486.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定