我正在一个个人项目中工作,使用角度,但我一直收到以下错误:
NullInjectorError: R3InjectorError(Environment Injector)[_HttpClient->_HttpClient]: NullInjectorError: No provider for _HttpClient!
我不知道为什么会发生这种情况,但这是我的
app.config.ts
:
import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
import { provideRouter } from '@angular/router';
import { routes } from './app.routes';
import { provideClientHydration, withEventReplay } from '@angular/platform-browser';
export const appConfig: ApplicationConfig = {
providers: [provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(routes), provideClientHydration(withEventReplay())]
};
我的其他组件 ts (menu-nav.component.ts) (app.config.ts) (main.ts):
import { Component, OnInit, Renderer2, ElementRef, HostListener } from '@angular/core';
import { HttpClient } from '@angular/common/http';
interface Traducciones {
[key: string]: {
inicio: string;
productos: string;
contacto: string;
};
}
@Component({
selector: 'menu-nav',
templateUrl: './menu-nav.component.html',
styleUrls: ['./menu-nav.component.scss'],
standalone: true,
})
export class MenuNavComponent implements OnInit {
idiomaActual: string = 'es';
traducciones: Traducciones = {};
constructor(private http: HttpClient, private renderer: Renderer2, private el: ElementRef) {}
import { Component } from '@angular/core';
import { MenuNavComponent } from './menu-nav/menu-nav.component';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
standalone: true,
imports: [MenuNavComponent],
})
export class AppComponent {}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app/app.component';
import { provideHttpClient } from '@angular/common/http';
bootstrapApplication(AppComponent, {
providers: [provideHttpClient()],
}).catch((err) => console.error(err));
在app.config.ts中将provideHttpClient()添加到appConfig:
export const appConfig: ApplicationConfig = {
providers: [
provideZoneChangeDetection({ eventCoalescing: true }),
provideRouter(routes),
provideClientHydration(withEventReplay()),
provideHttpClient()
]
};