angular - 在生产模式下构建 Angular 应用程序时出错

标签 angular compiler-errors production-environment httpmodule httpservice

当我想使用 prod 标志构建我的应用程序时,由于在我的一项服务中使用了 Http 模块,构建过程将失败。它无法解析此服务的参数。

这是代码示例

import { BadInput } from './../common/bad-input';
import { NotFoundError } from './../common/not-found-error';
import { AppError } from './../common/app-error';
import { Http } from '@angular/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import 'rxjs/add/observable/throw';

@Injectable()
export class DataService {
  constructor(private endpoint: string, private http: Http) { }

  private serverUrl:string ='http://example.com/api/'
  private url=this.serverUrl+this.endpoint;

  getAll() {
    return this.http.get(this.url)
      .map(response => response.json())
      .catch(this.handleError);
  }
private handleError(error: Response) {
    if (error.status === 400)
      return Observable.throw(new BadInput(error.json()));

    if (error.status === 404)
      return Observable.throw(new NotFoundError());

    return Observable.throw(new AppError(error))
  }
}

这是我使用 ng build --prod 时遇到的错误
ERROR in Error: Can't resolve all parameters for DataService in D:/src/app/services/data.service.ts: (?, [object Object]).
    at syntaxError (D:\node_modules\@angular\compiler\bundles\compiler.umd.js:1729:34)
    at CompileMetadataResolver._getDependenciesMetadata (D:\node_modules\@angular\compiler\bundles\compiler.umd.js:15965:35)
    at CompileMetadataResolver._getTypeMetadata (D:\node_modules\@angular\compiler\bundles\compiler.umd.js:15833:26)
    at CompileMetadataResolver._getInjectableMetadata (D:\node_modules\@angular\compiler\bundles\compiler.umd.js:15819:21)
    at CompileMetadataResolver.getProviderMetadata (D:\node_modules\@angular\compiler\bundles\compiler.umd.js:16110:40)
    at D:\node_modules\@angular\compiler\bundles\compiler.umd.js:16039:49
    at Array.forEach (<anonymous>)
    at CompileMetadataResolver._getProvidersMetadata (D:\node_modules\@angular\compiler\bundles\compiler.umd.js:15999:19)
    at CompileMetadataResolver.getNgModuleMetadata (D:\node_modules\@angular\compiler\bundles\compiler.umd.js:15654:50)
    at addNgModule (D:\node_modules\@angular\compiler\bundles\compiler.umd.js:24408:58)
    at D:\node_modules\@angular\compiler\bundles\compiler.umd.js:24419:14
    at Array.forEach (<anonymous>)
    at _createNgModules (D:\node_modules\@angular\compiler\bundles\compiler.umd.js:24418:26)
    at analyzeNgModules (D:\node_modules\@angular\compiler\bundles\compiler.umd.js:24293:14)
    at analyzeAndValidateNgModules (D:\node_modules\@angular\compiler\bundles\compiler.umd.js:24303:35)
    at AotCompiler.analyzeModulesAsync (D:\node_modules\@angular\compiler\bundles\compiler.umd.js:23937:46)

最佳答案

Http 已被弃用,取而代之的是 HttpClient,因此您可能需要查看以下内容:http://brianflove.com/2017/07/21/migrating-to-http-client/

同时,我还没有尝试过,但是如果您不使用 AOT 选项,您可能可以继续使用 Http 一段时间,该选项默认为 prod 构建打开。试试这个:

ng build --prod --aot=false

关于angular - 在生产模式下构建 Angular 应用程序时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49129538/

相关文章:

ruby-on-rails - 仅在生产中执行了验证,但 Rails 3.2.0 上的字段未被 field_with_errors div 包围

php - JSON.解析 : unexpected end of data at line 1 column 1 of the JSON data( Angular2 backend php server SQL)

angular - 如何使用关键帧在动画项目中正确创建和构造?

java - 用于开发和生产的不同 URL。 (重新加载静态内容)

c++ - 返回对象时没有匹配的调用函数

java - 如何解决 "cannot find symbol"包中的 Timeout.seconds() 方法生成的 "org. junit.rules.Timeout"错误?

Java 条件配置 (web.xml) - 开发/生产

angular - 我如何读取 Angular 2 中的配置文件条目?

Angular 类型错误 ('readdirp: root argument must be a string.' )

java - 编译失败-静态变量示例