You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular启动报错record.factory is not a function问题求助

问题原因及修复方案

1. 核心问题:服务类错误添加了@NgModule装饰器

你的APICommunicationService同时使用了@NgModule和@Injectable装饰器,这违反了Angular规范。@NgModule仅用于定义Angular模块,不能用于服务类。Angular会将带有@NgModule的类视为模块而非服务,导致注入时找不到服务工厂方法,从而抛出record.factory is not a function错误。

修复步骤:

完全移除服务类中的@NgModule相关代码,修改后的服务代码如下:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class APICommunicationService {
  private apiUrl = 'https://localhost:7023/api';

  constructor(private http: HttpClient) {}

  getData(): Observable<any> {
    return this.http.get<any>(`${this.apiUrl}/Test`); 
  }
}

2. 独立组件缺失必要模块导入

由于AppComponent是独立组件(standalone: true),它需要显式导入依赖的模块。你的服务依赖HttpClient,因此必须在组件中导入HttpClientModule(或Angular 14+推荐的provideHttpClient)。

修复步骤:

在AppComponent的@Component装饰器中添加imports数组:

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  standalone: true,
  imports: [HttpClientModule] // 新增此行
})

或者,如果你通过bootstrapApplication启动应用,也可以在启动配置中导入模块:

// main.ts
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app/app.component';
import { HttpClientModule } from '@angular/common/http';

bootstrapApplication(AppComponent, {
  imports: [HttpClientModule]
});

验证修复

完成上述修改后,重新启动应用。record.factory is not a function错误应该会消失,同时HttpClient能正常注入到服务中,API请求可正常执行。

内容的提问来源于stack exchange,提问作者Aethillium

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 19:44:56