【问题标题】:How to throw errors from one service and catch it in another component in Angular2如何从一个服务中抛出错误并在Angular2的另一个组件中捕获它
【发布时间】:2017-08-10 17:30:04
【问题描述】:

我已将 Firebase 用于身份验证。

这是验证码

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);
  }

因此,我不想打印错误,而是想抛出一个错误并在注册组件中捕获它,以便我可以使用该错误并向用户显示一条闪存消息。我怎样才能做到这一点?有人可以解决我的问题吗?

【问题讨论】:

    标签: angular firebase firebase-realtime-database firebase-authentication


    【解决方案1】:

    只需返回承诺,让调用者附加他们自己的处理程序:

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

    那么调用者可以这样做:

    this.authService.signupUser(email,password).catch(...);
    

    【讨论】:

      猜你喜欢
      • 2019-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-04
      • 1970-01-01
      • 1970-01-01
      • 2017-04-01
      • 1970-01-01
      相关资源
      最近更新 更多