Angular 7-build --prod失败,并显示以下错误:无法解析所有参数

尤金·基林(Eugene Kirin)

我使用Angular:7.2.10,当我尝试使用命令构建生产项目时:

ng b --prod

我有错误

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

我为带有3个参数的构造函数提供服务:

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

通过在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);
}

当我使用ng build它成功。

阿鲁安·哈达德(Aluan Haddad)

通过检查编译器发出的元数据可以隐式解决依赖关系。此元数据从参数的类型派生。

在运行时,角度喷射器检查该信息以确定要喷射的依赖项。具体来说,它为每个对应的参数寻找注册的提供程序。

由于尚未注册映射到为类型参数发出的元数据的提供程序,string因此查找失败,并且会收到错误消息。您可以为该类型注册一个提供程序,但鉴于使用的字符串有多广泛,这样做是不明智的。

但是,Angular的依赖项注入工具不限于此隐式解决方案。使用Injectdecorator和InjectionTokens的组合,可以实现您想要的。

api-token.ts

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

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

现在,您可以使用此令牌请求为特定参数解决此依赖关系。

数据服务

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

错误:无法解析“ \ node_modules \ @ angular-devkit \ build-angular \ src \ angular-cli-files \ models中的“ core-js / es7 / reflect”

Angular 4-失败:无法解析ActivatedRoute的所有参数:(?,?,?,?,?,?,?,?)

Angular 2错误:无法解析“ RouteParams”的所有参数

Angular2-错误:无法解析IconService的所有参数

Angular 5“ ng build”没有显示错误“ ng build --prod”有很多错误

Angular-无法解析组件ng build --prod的所有参数

无法解析DataService Angular的所有参数

找不到Angular 7 prod build i18n文件

错误:使用ng build --prod构建时无法解析Service的所有参数

Angular 7 Universal-无法解析路径

Angular 7 + Webpack:未捕获错误:无法解析e:(?)的所有参数

Angular无法解析SomeEffect的所有参数(?)

GoCD-无法执行Angular Build

在ng build --prod期间无法解析验证指令中的所有参数

未捕获的错误:在Angular 7和webpack中无法解析LoginComponent:(?)的所有参数

具有angular / cli v8的Angular build v7库

Angular-Prod Build无法生成唯一的哈希

从Angular2升级到Angular4后,ng build --prod失败

Angular 2 Jasmine 错误:无法解析 NgRedux 的所有参数

Angular 4 NG Build --Prod 对话框错误

Angular Router Guard 错误无法解析所有参数

Angular 无法注入 HttpClient 获取“无法解析 CustomerService 的所有参数”错误

Angular - 未捕获的错误:无法解析 HomeComponent 的所有参数:(?, ?, ?, ?)

Angular CLI 不包括 ng build --prod 上的所有包

Nativescript + Angular -> 错误:无法解析 ApplicationModule 的所有参数:(?)

预期有 2 个参数,但在 angular build prod 中得到 1 个参数

无法运行“ng build”Angular 12.1.0

Angular 7 + Ionic 4 build failing "TypeError: Cannot read properties of undefined (reading 'kind')"

Angular - 无法解析 NgZone 的所有参数