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

Angular使用--prod标志构建时出现运行时错误求助

解决Angular 2生产构建(ng build --prod)时的依赖注入错误

Hey there, let's tackle this frustrating issue you're facing! The split behavior between ng build (development mode with JIT compilation) and ng build --prod (production mode with AOT compilation) is the key clue here.

Production builds enable AOT (Ahead-of-Time) compilation, which enforces stricter dependency injection rules than the JIT compiler used in development. The error Cannot resolve all parameters for 'z'(?, ?, ?) means Angular's AOT compiler can't figure out how to inject dependencies for the class/service labeled z (this is almost certainly a minified name from the production build). Here's how to fix this step by step:

Common Fixes to Try

1. Ensure the z class has the @Injectable() decorator

In JIT mode, you might get away with skipping @Injectable() for services with constructor parameters, but AOT requires it explicitly. Add the decorator to your service class:

import { Injectable } from '@angular/core';

@Injectable() // Don't omit this!
export class YourActualServiceName { // 'z' is the minified name of this class
  constructor(
    private firstDependency: FirstService,
    private secondDependency: SecondService
  ) {}
}

2. Verify constructor parameters have valid type annotations

AOT relies on TypeScript type annotations to resolve dependencies. If you're using interfaces (which are erased during compilation) or untyped parameters, AOT can't resolve them.

  • ❌ Bad: Using an interface as a dependency
    // AppConfig is an interface, not a class
    constructor(private config: AppConfig) {}
    
  • ✅ Good: Use a class instead
    constructor(private config: AppConfigService) {}
    
  • ✅ Alternative: Use @Inject() with a token if you need to inject a value/interface
    import { Inject } from '@angular/core';
    
    constructor(@Inject('APP_CONFIG') private config: AppConfig) {}
    
    Don't forget to register the token in your module's providers array:
    providers: [
      { provide: 'APP_CONFIG', useValue: { apiUrl: 'https://your-api.com' } }
    ]
    

3. Fix circular dependencies

If two services depend on each other, JIT might handle it, but AOT will throw an error. Use forwardRef() to delay dependency resolution:

import { Inject, forwardRef } from '@angular/core';
import { OtherService } from './other.service';

@Injectable()
export class YourService {
  constructor(
    @Inject(forwardRef(() => OtherService)) private otherService: OtherService
  ) {}
}

4. Confirm all dependencies are registered in providers

In Angular 2, any service you inject must be declared in your module's providers array (or a component's providers if it's component-scoped). AOT won't automatically discover unregistered services.

5. Locate the actual z class (if minified)

The name z is a minified alias for your service. To find the original class:

  • Run ng build --prod --source-map to generate source maps, then use your browser's dev tools to map the minified z back to the original class.
  • Or temporarily disable minification with ng build --prod --optimization=false to see the original class name in the error.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 07:04:28