我有一个组件(参见:它是standalone个组件):

@Component({
  standalone: true, // <--- See here 
  selector: "app-login",
  imports: [FormsModule, CommonModule],
  templateUrl: "./login.component.html",
  styleUrl: "./login.component.css"
})
export class LoginComponent {
  constructor(private authService: AuthService) {}
}

服务是(请看,它要求HttpClient为injected):

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

@Injectable({
  providedIn: 'root',
})
export default class AuthService {
  constructor(private http: HttpClient) {} // <--- See here: if I remove this httpClient, it works.
}

它不起作用:

ERROR NullInjectorError: R3InjectorError(Standalone[e])[e -> e -> e -> e]: 
  NullInjectorError: No provider for e!

如果我从服务的构造函数中删除httpClient,它可以工作(但它什么也不做).在我看来,在服务中注入HttpClient不起作用.

有什么线索吗?

版本:17度角

PS:删除了大量详细信息:-)

推荐答案

你是不是像这样在app.config.ts人中提供了HttpClient人:

export const appConfig: ApplicationConfig = {
  providers: [provideRouter(routes), provideAnimations(), provideHttpClient()]
};

Angular相关问答推荐

Angular 信号不更新afterNextender()

如何在投影项目组周围添加包装元素?

要素存储更改根存储状态

[NullInjectorError]:R3InjectorError(Standalone[_AppComponent])[]:NullInjectorError:No provider for _HttpClient

在Angular 17的本地服务应用程序时禁用SSR

我需要取消订阅路由的可观察事件吗

完成行为主体

http 调用的 RxJs 数组

iss 声明无效 Keycloak

Angular:如何在根组件中使用 ngx-translate?

类继承支持

请添加@Pipe/@Directive/@Component 注解

带有 Angular 的 Font Awesome 5

visibility:hidden

什么是 Angular 4,我可以从哪里了解更多信息?

Angular Material 模态对话框周围的空白

ERROR 错误:StaticInjectorError(AppModule)[UserformService -> HttpClient]:

检测指令中输入值何时更改

Angular 2 - 全局 CSS 文件

将外部 CSS 加载到组件中