简体   繁体   English

Angular 7 - build --prod 失败并出现错误:无法解析所有参数

[英]Angular 7 - build --prod failed with error: Can't resolve all parameters for

I use Angular: 7.2.10 and when I try to build project for production with command:我使用 Angular: 7.2.10 并且当我尝试使用命令构建用于生产的项目时:

ng b --prod

I got error我有错误

ERROR in : Can't resolve all parameters for ApiService in ...

I have service with a constructor with 3 params:我有一个带有 3 个参数的构造函数的服务:

constructor(api: string, private _http: HttpClient, private httpUtils: HttpUtilsService) {
    this.api = `${api}/api`;        
  }

Which instantiates by factory defined at app.module.ts:它由在 app.module.ts 定义的工厂实例化:

{      
      provide: ApiService,
      useFactory: apiHost,
      deps: [Store, HttpClient, HttpUtilsService]
    }

apiHost接口主机

export function apiHost(store: Store<RemoteConfig>, http: HttpClient, httpUtils: HttpUtilsService) {
  let item: string = localStorage.getItem(environment.apiHost);

  //store.pipe(select(backendApiHost), take(1)).subscribe(api => item = api); // Todo not always read val!
  //console.log('ss: ' + item);
  return new ApiService(item, http, httpUtils);
}

When I use ng build it works successfully.当我使用ng build它可以成功运行。

Dependencies are implicitly resolved by examining the metadata emitted by the compiler.通过检查编译器发出的元数据来隐式解析依赖关系。 This metadata is derived from the types of the parameters.该元数据源自参数的类型。

At runtime, the angular injector inspects that information to determine which dependencies to inject.在运行时,角度注入器会检查该信息以确定要注入哪些依赖项。 Specifically, it looks for a registered provider for each corresponding parameter.具体来说,它为每个相应的参数寻找一个注册的提供者。

Since you haven't registered a provider that maps to the metadata emitted for a parameter of type string the lookup fails and you receive an error.由于您尚未注册映射到为string类型的参数发出的元数据的提供程序,因此查找失败并收到错误消息。 You could register a provider for that type but it would not be wise to do so given just how broadly strings are used.您可以为该类型注册一个提供程序,但鉴于字符串的使用范围如此广泛,这样做是不明智的。

However, Angular's dependency injection facilities are not limited to this implicit resolution.然而,Angular 的依赖注入工具并不限于这种隐式解析。 Using a combination of the Inject decorator and InjectionToken s you can achieve what you wish.使用Inject装饰器和InjectionToken的组合,您可以实现您的愿望。

api-token.ts api-token.ts

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

export const apiToken = new InjectionToken('api', {
  providedIn: 'root',
  value: 'myapi'
});

Now you can use this token to request that this dependency is resolved for a specific parameter.现在您可以使用此令牌请求为特定参数解析此依赖项。

data.service.ts数据服务.ts

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

import {apiToken} from './api-token';

@Injectable({providedIn: 'root'})
export class DataService {
   constructor(@Inject(apiToken) api: string) {}
}

Angular can't find a provider for api: string . Angular 找不到api: string的提供者。 In fact, you do not inject ApiService, you create it in the code here: return new ApiService(item, http, httpUtils) , so you don't need to define it in providers.实际上,您并没有注入 ApiService,而是在此处的代码中创建它: return new ApiService(item, http, httpUtils) ,因此您无需在提供程序中定义它。

i suggest you to remove the api variable from the constructor, pass it on the class methods, use the constructor only to pass injection.我建议您从构造函数中删除 api 变量,将其传递给类方法,仅使用构造函数来传递注入。


    constructor(public _http: HttpClient) { }

getApi(api: string) {
    this._http.get(api).toPromise()
}

then extends your api service and pass the uri in the parameter, i suggest you let _http public too, the api host cannot see the private http and these typing can fail a build prod然后扩展你的 api 服务并在参数中传递 uri,我建议你也让 _http 公开,api 主机看不到私有 http,这些输入可能会导致构建产品失败

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular-无法解析组件ng build --prod的所有参数 - Angular - Can't resolve ALL Parameters for component ng build --prod 错误:使用ng build --prod构建时无法解析Service的所有参数 - ERROR in : Can't resolve all parameters for Service when build with ng build --prod 无法解析 AngularFirestore (PROD Build) 的所有参数 - Can't resolve all parameters for AngularFirestore (PROD Build) 在ng build --prod期间无法解析验证指令中的所有参数 - Can't resolve all parameters in validation directive during ng build --prod 如何解决无法解决角度上的所有参数错误 - How to resolve Can't resolve all parameters error on angular Angular 4 - 失败:无法解析 ActivatedRoute 的所有参数:(?, ?, ?, ?, ?, ?, ?, ?) - Angular 4 - Failed: Can't resolve all parameters for ActivatedRoute: (?, ?, ?, ?, ?, ?, ?, ?) 带有 ionic 4 的自定义 angular 7 库 - 无法解决“../@ionic/angular/dist/core.ngfactory”构建产品错误 - Custom angular 7 library with ionic 4 - Can't resolve '../@ionic/angular/dist/core.ngfactory' build prod error Angular CLI 2错误无法通过AOT生成解析XXXXX的所有参数 - Angular cli 2 Error Can't resolve all parameters for XXXXX by AOT build 错误:无法解析Angular6中MenuController的所有参数 - ERROR in : Can't resolve all parameters for MenuController in Angular6 Angular 2 TypeScript错误:无法解析ProductsService的所有参数 - Angular 2 TypeScript Error: Can't resolve all parameters for ProductsService
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM