admin管理员组文章数量:1320627
I'm working in a personal project, using angular, but I receive the following error all the time:
NullInjectorError: R3InjectorError(Environment Injector)[_HttpClient->_HttpClient]: NullInjectorError: No provider for _HttpClient!
I don't know why this happens, but here is my 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())]
};
my others components ts (menu-navponent.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-navponent.html',
styleUrls: ['./menu-navponent.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-navponent';
@Component({
selector: 'app-root',
templateUrl: './appponent.html',
styleUrls: ['./appponent.scss'],
standalone: true,
imports: [MenuNavComponent],
})
export class AppComponent {}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app/appponent';
import { provideHttpClient } from '@angular/common/http';
bootstrapApplication(AppComponent, {
providers: [provideHttpClient()],
}).catch((err) => console.error(err));
I'm working in a personal project, using angular, but I receive the following error all the time:
NullInjectorError: R3InjectorError(Environment Injector)[_HttpClient->_HttpClient]: NullInjectorError: No provider for _HttpClient!
I don't know why this happens, but here is my 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())]
};
my others components ts (menu-navponent.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-navponent.html',
styleUrls: ['./menu-navponent.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-navponent';
@Component({
selector: 'app-root',
templateUrl: './appponent.html',
styleUrls: ['./appponent.scss'],
standalone: true,
imports: [MenuNavComponent],
})
export class AppComponent {}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app/appponent';
import { provideHttpClient } from '@angular/common/http';
bootstrapApplication(AppComponent, {
providers: [provideHttpClient()],
}).catch((err) => console.error(err));
Share
Improve this question
edited Jan 18 at 12:41
JSON Derulo
17.9k11 gold badges57 silver badges75 bronze badges
asked Jan 18 at 12:04
SemicolonSemicolon
1375 bronze badges
3
|
1 Answer
Reset to default 1In app.config.ts
add provideHttpClient()
to appConfig
:
export const appConfig: ApplicationConfig = {
providers: [
provideZoneChangeDetection({ eventCoalescing: true }),
provideRouter(routes),
provideClientHydration(withEventReplay()),
provideHttpClient()
]
};
本文标签: angularNullInjectorError R3InjectorErrorStack Overflow
版权声明:本文标题:angular - NullInjectorError: R3InjectorError - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742073971a2419295.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
provideHttpClient(),
– Bruno Rodrigues Commented Jan 18 at 12:11appConfig
when you don't use it inapp.config.ts
? – JSON Derulo Commented Jan 18 at 12:38