Я делаю проект в Angular 18, я хочу использовать httpClientModule, но он говорит мне, что он устарел, когда я хочу импортировать его непосредственно в компонент. Аналогично, когда я хочу импортировать httpClient в импорт одного и того же компонента, он говорит мне component imports must be standalone components, directives, pipes, or must be NgModules.
Я проводил расследование, и там было сказано, что решение состоит в том, чтобы поместить функцию provideHttpClient() в поставщики файла app.module.ts, но в моем случае у меня нет этого файла, у меня есть только app.config.ts и app.config.server.ts В какой из обоих мне следует ее поместить?
Содержимое обоих файлов следующее:
//app.config.ts
import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
import { provideRouter, withComponentInputBinding } from '@angular/router';
import { routes } from './app.routes';
import { provideClientHydration } from '@angular/platform-browser';
import { provideHttpClient } from '@angular/common/http';
export const appConfig: ApplicationConfig = {
providers: [provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(routes, withComponentInputBinding()), provideClientHydration()]
};
//app.config.server.ts
import { mergeApplicationConfig, ApplicationConfig } from '@angular/core';
import { provideServerRendering } from '@angular/platform-server';
import { appConfig } from './app.config';
import { provideHttpClient } from '@angular/common/http';
const serverConfig: ApplicationConfig = {
providers: [
provideServerRendering(),
]
};
export const config = mergeApplicationConfig(appConfig, serverConfig);





Содержимое app.config.ts также находится в app.server.ts-mergeApplicationConfig, поэтому вам нужно добавить ProvideHttpClient только в app.config.ts.
Содержимое app.config.ts переходит в bootstrapApplication в main.ts
//app.config.ts
import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
import { provideRouter, withComponentInputBinding } from '@angular/router';
import { routes } from './app.routes';
import { provideClientHydration } from '@angular/platform-browser';
import { provideHttpClient } from '@angular/common/http';
export const appConfig: ApplicationConfig = {
providers: [
provideZoneChangeDetection({ eventCoalescing: true }),
provideRouter(routes, withComponentInputBinding()),
provideClientHydration(),
provideHttpClient(), // <- changed here!
]
};