[英]How to throw errors from one service and catch it in another component in Angular2
I have used Firebase for authenticaton purpose. 我已将Firebase用于身份验证目的。
This is the code for authentication 这是验证代码
export class AuthService {
signupUser(email:string,password:string){
firebase.auth().createUserWithEmailAndPassword(email,password).catch((error)=>{
console.log(error);
})
}
This is code for signup in signup.Component.ts 这是signup.Component.ts中用于注册的代码
onSignup(form:NgForm){
const email=form.value.email;
const password=form.value.password;
this.authService.signupUser(email,password);
}
So instead of printing an error, I would like to throw an error and catch it in signup component so that I can use that error and display a flash message to the user. 因此,我不想抛出错误,而是想抛出一个错误并将其捕获到注册组件中,以便可以使用该错误并向用户显示一条即时消息。 How can I do that?
我怎样才能做到这一点? Can somebody please solve my problem?
有人可以解决我的问题吗?
Just return the promise, and let the caller attach their own handlers: 只需返回promise,然后让调用方附加自己的处理程序即可:
signupUser(email:string,password:string){
return firebase.auth().createUserWithEmailAndPassword(email,password);
}
Then the caller can do: 然后,呼叫者可以执行以下操作:
this.authService.signupUser(email,password).catch(...);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.