简体   繁体   English

以角度返回错误时的最佳做法是什么

[英]What is the best practice when returning errors in angular

I've an angular application.我有一个角度应用程序。 In this application I've an AuthService .在这个应用程序中,我有一个AuthService This AuthService login one user against the firebase auth.AuthService针对AuthService身份验证登录一个用户。

My question is the following:我的问题如下:

Do you think it's the responsability of the service to navigate to a success page if logged successfully?如果登录成功,您认为导航到成功页面是服务的责任吗? Or should it be the component calling the service?还是应该是调用服务的组件?

And if there is an error(like invalid password in my case), how would you return this information to the component to display it to the user?如果出现错误(如我的情况下无效密码),您将如何将此信息返回给组件以将其显示给用户?

My service:我的服务:

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { auth } from 'firebase/app';
import { AngularFireAuth } from '@angular/fire/auth';
import { User } from 'firebase';

@Injectable({
    providedIn: 'root'
})
export class AuthService {
    user: User;
    constructor(public afAuth: AngularFireAuth, public router: Router) {
        this.afAuth.authState.subscribe(user => {
            if (user) {
                this.user = user;
                localStorage.setItem('user', JSON.stringify(this.user));
            } else {
                this.user = null;
                localStorage.setItem('user', null);
            }
        });
    }

    async login(email: string, password: string) {
        let result = await this.afAuth.auth.signInWithEmailAndPassword(
            email,
            password
        );
        this.router.navigate(['...']); 
    }
}

My component:我的组件:

import { Component } from '@angular/core';
import { AuthService } from '../auth.service';
import { NgForm } from '@angular/forms';

@Component({
    selector: 'app-login',
    templateUrl: './login.component.html',
    styleUrls: ['./login.component.css']
})
export class LoginComponent  {

    constructor(private authService: AuthService) { }

    onSubmit(form: NgForm) {
        this.authService.login(form.value.email, form.value.password);
    }

}

To answer for your question回答你的问题

Do you think it's the responsability of the service to navigate to a success page if logged successfully?如果登录成功,您认为导航到成功页面是服务的责任吗? Or should it be the component calling the service?还是应该是调用服务的组件?

For clean code respective I think you need to handle navigate logic in the service because from time to time your component will growth and your code base is very hard to maintain.对于相应的干净代码,我认为您需要处理服务中的导航逻辑,因为有时您的组件会增长并且您的代码库很难维护。 Or I would suggest you take a research on NGRX library they have 1 layer call effect so you can do side effect logic for your app in there and the service class only do calling API.或者我建议您对 NGRX 库进行研究,它们具有 1 层调用效果,因此您可以在其中为您的应用程序执行副作用逻辑,而服务类仅调用 API。

And if there is an error(like invalid password in my case), how would you return this information to the component to display it to the user?如果出现错误(如我的情况下无效密码),您将如何将此信息返回给组件以将其显示给用户?

Same as my option above.和我上面的选项一样。 And would suggest you go for side effect approach because API error will be handle inside effect class too并建议您采用副作用方法,因为 API 错误也将在效果类中处理

Below is the sample of my app.以下是我的应用程序示例。 When I received success login action I will redirect user login to portal page.当我收到成功登录操作时,我会将用户登录重定向到门户页面。 To fully understand this pattern you can take a look at this picture要完全理解这种模式,您可以看一下这张图片

在此处输入图片说明

A component is dispatch an action.一个组件是分派一个动作。 Action will be handle by effect if we need to some side effect (like in this example is redirect user to poratal page if they login success).如果我们需要一些副作用(比如在这个例子中,如果用户登录成功,将用户重定向到门户页面),动作将被效果处理。 Then the reducer will calculate new state base on action type and the data coming from component or effect side then it will update the UI to reflect the change.然后,reducer 将根据操作类型和来自组件或效果端的数据计算新状态,然后它会更新 UI 以反映更改。

@Effect({dispatch: false})
  loginSuccess$ = this
    .actions$
    .pipe(ofType(authAction.LoginActionTypes.LOGIN_SUCCESS), tap(() => this.router.navigate(['/portal'])));

You can take a look of my full sample here and NGRX lib您可以在此处查看我的完整示例和NGRX

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM