2017-02-23 147 views
0

我無法找到我的錯誤。Angular2將服務注入其他服務

app.module.ts 
    ... 
    providers: [ValidateService,AuthService] 
    ... 

我做我的register.component.ts如下:

import {AuthService} from '../../services/auth.service'; 
... 
constructor(private _validateService: ValidateService, 
       private _fms: FlashMessagesService, 
       private _authService: AuthService, 
       private _router: Router 
       ) { } 
... 
ngOnInit() { 
     this._authService.uniqueUser({username:'zomh'}).subscribe(data => { 
     console.log("data.success: "+data.success);  
     if(!data.success) { // Username already exists  
     console.log('exists'); 
     } 
     else { 
     console.log('does not exist'); 
     } 
     }); 
    } 

按預期工作的用戶已經在數據庫中,因此我得到了一個用戶在控制檯中存在。

我不漂亮,漂亮,多在我validate.service.ts同樣的事情(我打破了下來,這一點):

import { AuthService } from './auth.service'; 
import { Injectable } from '@angular/core'; 
import { FormControl } from '@angular/forms'; 



@Injectable() 
export class ValidateService { 

    constructor(public _authService: AuthService) { } 

    validateRegister(user) { 
    if(user.name == undefined || user.email == undefined || user.username == undefined || user.password == undefined) 
     return false; 
    else 
     return true; 
    } 

    validateEmailPattern(c: FormControl) { 
    const re = /^(([^<>()\[\]\\.,;:\[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,}))$/; 

    if (re.test(c.value)) 
     return null; 
    else 
     return {invalidPattern:true}; 

    } 
    validateUsernamePattern(c: FormControl) { 
    const re = /^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/ 

    if (re.test(c.value)) 
     return null; 
    else 
     return {invalidPattern:true}; 

    } 
    validateUsernameIsUnique (c: FormControl) { 

    let ret:any; 
    if (c.value.length >= 3) 
    { 
     console.log(c.value); 
     this._authService.uniqueUser({username:'zomh'}).subscribe(data => { 

     if(!data.success) { // Username already exists  
     console.log('call from service: exists'); 
     } 
     else { 
     console.log('call from service: does not exist'); 
     } 
     }); 

    } 

    return {usernameIsTaken:true}; 
    } 


} 

但在這裏我得到了Cannot read property _authService of undefined異常 enter image description here

對我來說,它看起來像服務沒有正確注入。但我找不到我的錯誤。

更新1: 所以我沒有將auth服務調用複製到構造函數及其工作中。所以它必須是這個。相關的錯誤(?)我無法從構造函數以外的任何其他方法獲取this._authService的值?

@Injectable() 
export class ValidateService { 

    constructor(private _authService: AuthService) { 

     this._authService.uniqueUser({ username: 'zomh' }).subscribe(data => { 

     if (!data.success) { // Username already exists  
      console.log('call from service: exists'); 
     } 
     else { 
      console.log('call from service: does not exist'); 
     } 
     }); 
    } 

回答

0

閱讀的文章之後,我改寫了我的方法爲實例方法:

validateUsernameIsUnique = (c: FormControl) => { 

    let ret: any; 
    if (c.value.length >= 3) {  
     this._authService.uniqueUser({ username: c.value }).subscribe(data => { 

     if (!data.success) { // Username already exists  
      console.log('call from service: exists'); 
     } 
     else { 
      console.log('call from service: does not exist'); 
     } 
     }); 

    } 
... 

它解決了這一問題。我仍然不確定爲什麼要這樣做,但隨時可以添加知識

0

我不認爲你可以有一個新的生產線@Injectableexport class ValidateService {

之間沒有這一行試試吧。

+0

刪除了新行。不幸的是仍然是相同的未定義的錯誤 – user3169083

+0

是你的'validateUsernameIsUnique'方法的所有代碼?因爲如果是這樣,它看起來像你的括號沒有正確匹配。 – Pytth

+0

我確實更新了主帖,所以你可以看到ValidaiteService的完整代碼 – user3169083