Angular 5 HttpClient" ERROR TypeError:赋值给常量变量"只有-prod但在调试中工作

时间:2018-02-09 20:33:37

标签: angular angular-httpclient

如果我使用ng serve运行我的应用程序,一切都按预期工作,并且不会抛出任何错误。 如果我使用ng serve -prod运行相同的代码(即使使用相同的后端服务器),每次调用我的Rest服务都会失败

main.30347c9fa4d6d318dcf3.bundle.js:1 ERROR TypeError: Assignment to constant variable.
at s (main.30347c9fa4d6d318dcf3.bundle.js:1)
at XMLHttpRequest.l (main.30347c9fa4d6d318dcf3.bundle.js:1)
at e.invokeTask (polyfills.ee89d2044fe261e27311.bundle.js:1)
at Object.onInvokeTask (main.30347c9fa4d6d318dcf3.bundle.js:1)
at e.invokeTask (polyfills.ee89d2044fe261e27311.bundle.js:1)
at t.runTask (polyfills.ee89d2044fe261e27311.bundle.js:1)
at t.invokeTask [as invoke] (polyfills.ee89d2044fe261e27311.bundle.js:1)
at _ (polyfills.ee89d2044fe261e27311.bundle.js:1)
at XMLHttpRequest.k (polyfills.ee89d2044fe261e27311.bundle.js:1)

我可以在不使用-prod的情况下构建应用程序,然后部署它,它也能正常工作。 我认为这与uglify-es或WebPack有关,但我并不完全确定。这是我的package.json依赖项。有什么想法吗?

依赖关系:

    "@angular/animations": "^5.2.4",
    "@angular/cdk": "^5.1.1",
    "@angular/common": "^5.2.4",
    "@angular/compiler": "^5.2.4",
    "@angular/core": "^5.2.4",
    "@angular/forms": "^5.2.4",
    "@angular/http": "^5.2.4",
    "@angular/material": "^5.1.1",
    "@angular/platform-browser": "^5.2.4",
    "@angular/platform-browser-dynamic": "^5.2.4",
    "@angular/router": "^5.2.4",
    "bootstrap": "4.0.0",
    "commonjs": "0.0.1",
    "copyfiles": "^1.2.0",
    "crypto-js": "^3.1.9-1",
    "hammerjs": "^2.0.8",
    "material-design-icons": "^3.0.1",
    "postcss-merge-rules": "^2.1.2",
    "requirejs": "^2.3.5",
    "rxjs": "^5.5.6",
    "zone.js": "^0.8.20"

devDependencies:

    "@angular/cli": "^1.6.8",
    "@angular/compiler-cli": "^5.2.4",
    "@angular/language-service": "^5.2.4",
    "@types/jasmine": "^2.8.6",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "^8.5.9",
    "angular-ide": "^0.9.39",
    "codelyzer": "^4.1.0",
    "jasmine-core": "~2.8.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.4.1",
    "karma-jasmine": "^1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "^5.2.4",
    "ts-node": "~3.3.0",
    "tslint": "~5.8.0",
    "typescript": "2.6.2",
    "webpack-concat-plugin": "^2.4.2"

这是我调用我的休息服务的代码,它永远不会回来。

  /// get data with request parameters
  /// (e.g.) serverRoot/Parts?uidPart=52640-aof098-f9f-f8f-fff-990987
  getData_With_Request_Parameters(ep: EndPoints, params: Map<string, string>) {
    console.log('Creating Path');
    let path: string = this.serverRoot + '/' + this.getEndPoint(ep) + '?';
    console.log('creating param map');

    // append all of the parameters to the url
    for (const key of params.keys()) {
      path += key + '=' + params.get(key) + '&';
    }
    console.log('altering the path');
    // remove the last '&'
    path = path.slice(0, path.length - 1);
    console.log('sending get to path');
    return this.http.get(path).map((res: Response) => res);
  }

这是我的回调,永远不会被执行

  this.restService.getData_With_Request_Parameters(EndPoints.AuthenticateUser, data).subscribe(res => {
        console.log('INSIDE REST CALLBACK ' + email);
          if (res &&  res.toString() === 'true') {
            this.authenticated = true;
            this.accessCode = hashCode;
            this.loginError = '';
          } else {
              this.loginError = 'Login Failed.';
              this.authenticated = false;
              this.accessCode = '';
          }
          this.isLoggingIn = false;
      console.log(res);
      }, err => {this.isLoggingIn = false; console.log(err); this.loginError = 'Network Error. The server may be inaccessible.';  }
    );

这是我的ng -v

Angular CLI: 1.6.8
Node: 8.9.4
OS: win32 x64
Angular: 5.2.4
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

@angular/cdk: 5.1.1
@angular/cli: 1.6.8
@angular/material: 5.1.1
@angular-devkit/build-optimizer: 0.0.42
@angular-devkit/core: 0.0.29
@angular-devkit/schematics: 0.0.52
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.9.8
@schematics/angular: 0.1.17
typescript: 2.6.2
webpack-concat-plugin: 2.4.2
webpack-uglify-js-plugin: 1.1.9
webpack: 3.10.0

1 个答案:

答案 0 :(得分:3)

根据this discussion,您可以将target设置更改为位于根目录的es5文件中的tsconfig.json。矿业公司在变革后工作。 我的配置:

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "baseUrl": "src",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": ["node_modules/@types"],
    "lib": ["es2016", "dom"]
  }
}