In angular library – Error: NG0203: inject() must be called from an injection context such as a constructor, a factory function, a field initialize

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

In angular library - Error: NG0203: inject() must be called from an injection context such as a constructor, a factory function, a field initialize

问题

我在将我的项目从 Angular 9 升级到 15 后遇到以下问题。任何帮助都将不胜感激。我已经卡在这里超过2周。

在 Angular 库中的 app-lib.module.ts 文件中:

import { HTTP_INTERCEPTORS, HttpClientModule} from '@angular/common/http';
providers: [
    {
      provide: HTTP_INTERCEPTORS,
      useClass: HttpService,
      multi: true
    },
],

在我的 Angular 库中的 hero.service.ts 文件中:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class HeroService {
  texting: string = 'Wow!';
  constructor(private http: HttpClient) { }

  getResult() {
    return 'Wow!!!!!!';
  }
}

如果我像上面的代码一样在构造函数中包括 (private http: HttpClient),则会出现以下错误。如果将其移除,应用程序可以正常运行。

Error: Uncaught (in promise): Error: NG0203: inject() 必须从注入上下文中调用,如构造函数、工厂函数、字段初始化程序或与 `EnvironmentInjector#runInContext` 一起使用的函数。更多信息请查看 https://angular.io/errors/NG0203
Error: NG0203: inject() 必须从注入上下文中调用,如构造函数、工厂函数、字段初始化程序或与 `EnvironmentInjector#runInContext` 一起使用的函数。

我已经提供了代码的翻译,如您所需。

英文:

After updating one of my project from angular 9 to 15 I'm getting below issue. any help would be appreciated. I'm stuck in it for more then 2 weeks.

app-lib.module.ts in the angular library.

import { HTTP_INTERCEPTORS, HttpClientModule} from '@angular/common/http';
providers: [
    {
      provide: HTTP_INTERCEPTORS,
      useClass: HttpService,
      multi: true
    }, ],

hero.service.ts in my angular library

import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class HeroService {
  texting: string = 'Wow!';
  constructor(private http: HttpClient) { }

  getResult() {
    return 'Wow!!!!!!';
  }
}

getting below error if in include (private http: HttpClient) in the constructor like above code. if i remove it app is working fine.

Error: Uncaught (in promise): 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`. Find more at https://angular.io/errors/NG0203
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`.

After updating one of my project from angular 9 to 15 I'm getting below issue. any help would be appreciated. I'm stuck in it for more then 2 weeks.

答案1

得分: 1

我升级应用程序时遇到了类似的问题,从 9.1 升级到 15.2.9。当我将版本更改为 15.1.3 时,我的错误得以解决。检查你的 package-lock.json 文件以查找 Angular 版本中是否存在冲突。在我的情况下,我的某个库正在使用 15.1.3 作为它的依赖项,导致了这个问题。(@angular/core 版本冲突)

英文:

I faced the similar issue when I tried upgrading my application from 9.1 to 15.2.9 . My Error got fixed when I changed the version to 15.1.3 . Check your package-lock.json file to figure out any conflict present in the angular versions. In my case one of my libraries was using 15.1.3 as it's dependency which caused this issue. (@angular/core version conflict)

huangapple
  • 本文由 发表于 2023年5月22日 22:45:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76307360.html
匿名

发表评论

匿名网友

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

确定