关键字 'private' 已保留 (35:12) 您可能需要适当的加载程序来处理此文件类型

The keyword 'private' is reserved (35:12) You may need an appropriate loader to handle this file type

我的 Angular 5 应用程序现在遇到一个奇怪的问题。错误堆栈如下。

Failed to compile.

./src/app/registration/registration.component.ts Module parse failed: The keyword 'private' is reserved (35:12) You may need an appropriate loader to handle this file type. | }; | } | constructor(private, fb, FormBuilder); | { | this.form = fb.group({ @ ./src/app/app.module.ts 16:0-78 @ ./src/main.ts @ multi webpack-dev-server/client?http://0.0.0.0:0 ./src/main.ts

这是我的代码。

export class RegistrationComponent implements OnInit {

  form;
  constructor(private fb: FormBuilder) {
    this.form = fb.group({
      firstName: ['', Validators.required],
      lastName: ['', Validators.required],
      email: ['', [Validators.required, isEmailValid('email')]],
      password: ['', Validators.required],
      confirmPassword: ['', Validators.required]
    }, { validator: compareValidator('password', 'confirmPassword') });
  }

  function compareValidator(control1, control2) {
    return form => {
      if (form.controls[control1].value !== form.controls[control2].value) {
        return { compareResult: true };
      }
    }
  }

  function isEmailValid(control) {
    return control => {
      var regex = /^(([^<>()\[\]\.,;:\s@"]+(\.[^<>()\[\]\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
      return regex.test(control.value) ? null : { invalidEmail: true };
    }
  }

  ngOnInit() {
  }

}

谁能帮我找出问题所在?无论如何,如果您需要查看我的软件包版本,我附上了我的 package.json 文件。

{
  "name": "my-angular5-app",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^5.0.5",
    "@angular/cdk": "^5.0.0-rc0",
    "@angular/common": "^5.0.5",
    "@angular/compiler": "^5.0.5",
    "@angular/core": "^5.0.5",
    "@angular/forms": "^5.0.5",
    "@angular/http": "^5.0.5",
    "@angular/material": "^5.0.0-rc0",
    "@angular/platform-browser": "^5.0.5",
    "@angular/platform-browser-dynamic": "^5.0.5",
    "@angular/router": "^5.0.5",
    "core-js": "^2.5.1",
    "rxjs": "^5.5.3",
    "zone.js": "^0.8.18"
  },
  "devDependencies": {
    "@angular/cli": "^1.5.5",
    "@angular/compiler-cli": "^5.0.5",
    "@angular/language-service": "^5.0.5",
    "@types/jasmine": "~2.5.53",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "^6.0.92",
    "codelyzer": "~3.2.0",
    "jasmine-core": "~2.6.2",
    "jasmine-spec-reporter": "~4.1.0",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.1.1",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "^1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~3.2.0",
    "tslint": "~5.7.0",
    "typescript": "~2.4.2"
  }
}

有时候一件小事就让你摸不着头脑,这个就是这样。我不知道为什么我会犯这么小的错误。只需删除 class 之外的两个函数即可解决问题。只是与其他可能遇到相同情况的人分享。

export class RegistrationComponent implements OnInit {

  form;
  constructor(private fb: FormBuilder) {
    this.form = fb.group({
      firstName: ['', Validators.required],
      lastName: ['', Validators.required],
      email: ['', [Validators.required, isEmailValid('email')]],
      password: ['', Validators.required],
      confirmPassword: ['', Validators.required]
    }, { validator: compareValidator('password', 'confirmPassword') });
  }

  ngOnInit() {
  }

}

function compareValidator(control1, control2) {
  return form => {
    if (form.controls[control1].value !== form.controls[control2].value) {
      return { compareResult: true };
    }
  }
}

function isEmailValid(control) {
  return control => {
    var regex = /^(([^<>()\[\]\.,;:\s@"]+(\.[^<>()\[\]\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    return regex.test(control.value) ? null : { invalidEmail: true };
  }
}

如果您删除关键字 "function" 并在 class 中使用定义您的函数,您将收到一个错误,因为 isEmailValid 未定义