簡體   English   中英

如何修復錯誤錯誤:未捕獲(承諾):NullInjectorError:StaticInjectorError(AppModule)[HomeComponent - > HttpHeaders]

[英]How to fix ERROR Error: Uncaught (in promise): NullInjectorError: StaticInjectorError(AppModule)[HomeComponent -> HttpHeaders]

在使用 GET 方法從 API 調用中獲取時,我遇到了這樣的問題

core.js:6014 錯誤錯誤:未捕獲(承諾中):NullInjectorError:StaticInjectorError(AppModule)[HomeComponent -> HttpHeaders]:StaticInjectorError(平台:核心)[HomeComponent -> HttpHeaders]:NullInjectorError:沒有HttpHeaders的提供者:NullInjectorError:StaticInjectorError (AppModule)[HomeComponent -> HttpHeaders]:StaticInjectorError(平台:核心)[HomeComponent -> HttpHeaders]:NullInjectorError。 沒有 HttpHeaders 的提供者。 在 NullInjector:get (core.js:855) 在 resolveToken (core.js:17513) 在 tryResolveToken (core.js.17439) 在 StaticInjector:get (core.js:17265) 在 resolveToken (core.js:17513) 在tryResolveToken (core.js.17439) at StaticInjector:get (core.js:17265) at resolveNgModuleRef_:get (core.js:31577) at resolveDep (core.js:32142) at resolvePromise (zone-evergreen.js:797) 在 resolvePromise (zone-evergreen.js:754) 在 zone-evergreen.js.858 在 ZoneDelegate:invokeTask (zone-evergreen.js.391) 在 Object:onInvokeTask (core.js. 39679) 在 ZoneDelegate:invokeTask (zone-evergreen.js.390) 在 Zone:runTask (zone-evergreen.js:168) 在 drainMicroTaskQueue (zone-evergreen.js:559)

代碼:

return this.http
      .get('http://localhost/advanced/frontend/web/leftside-menu/get-left-menu')
      .subscribe(
        (result) => { }
      );

您是否在app.module.ts中導入了HttpClientModule 如果沒有,那么把這行代碼..

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

@NgModule({  
            imports: [ BrowserModule, FormsModule, HttpClientModule ],
            declarations: [ ]
           })

在 app.module.ts 中導入 HttpClientModule

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

@NgModule({  
   imports: [ BrowserModule, FormsModule, HttpClientModule ],
   declarations: [ ]
})

在你的 service.ts 文件中導入 HttpClient

import { MyModel } from './my-model.model'
export class UserMaterService {
  list: MyModel[];
  constructor(private http: HttpClient) { }
  this.http.get(url).toPromise().then(res => this.stringData = res as listData[]);
}

此外,如果出現這種情況,請檢查您在任何組件的構造函數中編寫的內容。

例如,我出於某種原因放了另一個組件,所以這是錯誤的當我刪除它時,它開始工作

 constructor( private chatRoomsService: ChatRoomService, private router: Router, private route: ActivatedRoute private messageComponent: MessageComponent //it has to be erased ) { }

我曾經遇到過同樣的問題,在這種情況下,您必須確保已在app.module.ts文件中導入httpclient模塊,如果有的話,請在 app.module.ts 中導入您的組件,即app.module.ts並添加此組件在提供者中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM