0

我已將Firebase用於authenticaton目的。如何從一個服務中拋出錯誤並將其捕獲到Angular2的另一個組件中

這是驗證

export class AuthService { 

     signupUser(email:string,password:string){ 
     firebase.auth().createUserWithEmailAndPassword(email,password).catch((error)=>{ 
      console.log(error); 
     }) 
     } 

代碼這是signup.Component.ts

onSignup(form:NgForm){ 
    const email=form.value.email; 
    const password=form.value.password; 
    this.authService.signupUser(email,password); 
    } 

因此,而不是印刷錯誤代碼註冊,我想拋出一個錯誤,在註冊組件中捕獲它,以便我可以使用該錯誤並向用戶顯示一條Flash消息。我怎樣才能做到這一點?有人可以解決我的問題嗎?

回答

1

剛剛回歸的諾言,並讓來電者附上自己的處理程序:

signupUser(email:string,password:string){ 
    return firebase.auth().createUserWithEmailAndPassword(email,password); 
} 

然後調用者可以這樣做:

this.authService.signupUser(email,password).catch(...); 
相關問題