简体   繁体   English

Angular 8:错误:未捕获(承诺):NullInjectorError:StaticInjectorError(AppModule)

[英]Angular 8 : Error: Uncaught (in promise): NullInjectorError: StaticInjectorError(AppModule)

I have an angular 8 app, where I am getting the following error :我有一个 angular 8 应用程序,出现以下错误:

core.js:4002 ERROR Error: Uncaught (in promise): NullInjectorError: StaticInjectorError(AppModule)[ScreenComponent -> MetadataUtil]: 
  StaticInjectorError(Platform: core)[ScreenComponent -> MetadataUtil]: 
    NullInjectorError: No provider for MetadataUtil!
NullInjectorError: StaticInjectorError(AppModule)[ScreenComponent -> MetadataUtil]: 
  StaticInjectorError(Platform: core)[ScreenComponent -> MetadataUtil]: 
    NullInjectorError: No provider for MetadataUtil!
    at NullInjector.push../node_modules/@angular/core/fesm5/core.js.NullInjector.get (core.js:725)
    at resolveToken (core.js:11918)
    at tryResolveToken (core.js:11862)
    at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:11764)
    at resolveToken (core.js:11918)
    at tryResolveToken (core.js:11862)
    at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:11764)
    at resolveNgModuleDep (core.js:20234)
    at NgModuleRef_.push../node_modules/@angular/core/fesm5/core.js.NgModuleRef_.get (core.js:20905)
    at injectInjectorOnly (core.js:628)
    at resolvePromise (zone.js:852)
    at resolvePromise (zone.js:809)
    at zone.js:913
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:423)
    at Object.onInvokeTask (core.js:26247)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:422)
    at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:195)
    at drainMicroTaskQueue (zone.js:601)
    at ZoneTask.push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (zone.js:502)
    at invokeTask (zone.js:1693)

The following is the code:以下是代码:

MetadataUtil.ts元数据实用程序

export class MetadataUtil {

    constructor(
        private http: HttpClient,
        private httpCache: HttpCacheService
    ) { }

    postData() {
       //...
    }
}
ScreenComponent.ts

import { MetadataUtil } from './MetadataUtil.ts';


@Component({
    selector: 'comp1',
    templateUrl: `./comp1.component.html`,
    styleUrls: ['./comp1.component.scss']
    , encapsulation: ViewEncapsulation.None
})
@Injectable({
    providedIn: 'root'
})

@NgModule({
    imports: [SharedModule, AngularBootstrapToastsModule]
})

export class ScreenComponent implements OnInit {
    constructor(
        private metadata: MetadataUtil
    ) { }
    
    getData(){
       this.metadata.postData();

    } 

}


So postData method in the MetadataUtil.ts file was static earlier and the ScreenComponent was using it as an import.因此 MetadataUtil.ts 文件中的 postData 方法之前是静态的,并且 ScreenComponent 将其用作导入。 I changed it not to be static and changed the reference in the ScreenComponent, and.我将其更改为非静态并更改了 ScreenComponent 中的引用,并且。 this error started coming up.这个错误开始出现。 I've seen similar posts online and people suggesting solutions for this, But I have not been able to understand what this error is about and why this might have been thrown.我在网上看到过类似的帖子,人们为此提出了解决方案,但我一直无法理解这个错误是关于什么以及为什么会抛出这个错误。 Could somebody explain me what this error is please?有人可以解释一下这个错误是什么吗? Thanks谢谢

According to latest versions you should use HttpClientModule instead of HttpModule根据最新版本,您应该使用HttpClientModule而不是HttpModule

Import like this像这样导入

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

@NgModule({
  declarations: [ ],
  imports: [
    ......
    HttpClientModule
  ]
....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误错误:未捕获(承诺):NullInjectorError:StaticInjectorError(AppModule)[FormBuilder] - ERROR Error: Uncaught (in promise): NullInjectorError: StaticInjectorError(AppModule)[FormBuilder] 未捕获(承诺):NullInjectorError:StaticInjectorError(AppModule)[CdkConnectedOverlay-> Overlay] - Uncaught (in promise): NullInjectorError: StaticInjectorError(AppModule)[CdkConnectedOverlay -> Overlay] 角度6:未捕获(承诺):错误:StaticInjectorError(AppModule)[RoleGuardService] - Angular 6: Uncaught (in promise): Error: StaticInjectorError(AppModule)[RoleGuardService] 角度7:错误错误:未捕获(承诺):错误:StaticInjectorError(AppModule)[MenuWidgetComponent-> TripsMenu]: - Angular 7 : ERROR Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[MenuWidgetComponent -> TripsMenu]: 如何修复错误错误:未捕获(承诺):NullInjectorError:StaticInjectorError(AppModule)[HomeComponent - > HttpHeaders] - How to fix ERROR Error: Uncaught (in promise): NullInjectorError: StaticInjectorError(AppModule)[HomeComponent -> HttpHeaders] core.js:4002 错误错误:未捕获(承诺中):NullInjectorError:StaticInjectorError(AppModule)[Table -> FilterService]: - core.js:4002 ERROR Error: Uncaught (in promise): NullInjectorError: StaticInjectorError(AppModule)[Table -> FilterService]: 错误:未捕获(承诺):NullInjectorError:R3InjectorError(AppModule)Angular 13 - Error: Uncaught (in promise): NullInjectorError: R3InjectorError(AppModule) Angular 13 未捕获(承诺):错误:StaticInjectorError(AppModule)[选项] - Uncaught (in promise): Error: StaticInjectorError(AppModule)[options] 未捕获(承诺):错误:StaticInjectorError(AppModule)[AuthServiceProvider-> HttpHeaders]: - Uncaught (in promise): Error: StaticInjectorError(AppModule)[AuthServiceProvider -> HttpHeaders]: 未捕获(承诺):错误:StaticInjectorError(AppModule)[employeService-> Http]: - Uncaught (in promise): Error: StaticInjectorError(AppModule)[employeService -> Http]:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM