Angular使用--prod标志构建时出现运行时错误求助
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
Don't forget to register the token in your module'simport { Inject } from '@angular/core'; constructor(@Inject('APP_CONFIG') private config: AppConfig) {}providersarray: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-mapto generate source maps, then use your browser's dev tools to map the minifiedzback to the original class. - Or temporarily disable minification with
ng build --prod --optimization=falseto see the original class name in the error.
内容的提问来源于stack exchange,提问作者Tomas Marik

