2017-12-02 208 views
0

我在Angular 5應用程序中遇到了一個奇怪的問題。錯誤堆棧如下。關鍵字'private'被保留(35:12)您可能需要一個合適的加載程序來處理此文件類型

無法編譯。

./src/app/registration/registration.component.ts模塊解析失敗: 關鍵字「私人」被保留(35:12)您可能需要適當的 加載器來處理此文件類型。 | }; | } |構造函數(私有, 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 = /^(([^<>()\[\]\\.,;:\[email protected]"]+(\.[^<>()\[\]\\.,;:\[email protected]"]+)*)|(".+"))@((\[[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" 
    } 
} 

回答

0

有時候一個小問題可能會讓你撓頭,這是一種類型。我不知道爲什麼我犯了這麼小的錯誤。只是在班級之外移除這兩個功能解決了問題。只需與其他可能以相同情況結束的人分享即可。

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 = /^(([^<>()\[\]\\.,;:\[email protected]"]+(\.[^<>()\[\]\\.,;:\[email protected]"]+)*)|(".+"))@((\[[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 }; 
    } 
} 

如果刪除關鍵字「功能」和使用定義在類中的功能,你會isEmailValid未定義得到一個錯誤。

相關問題