Я застрял с BehaviorSubject в angular

Я создал централизованную службу обработки ошибок, используя BehaviorSubject в Angular v 17. Она не работает должным образом!

Проблемными местами являются: NotificationService -> централизованный обработчик ошибок. NotificationComponent -> Многоразовое удобное для пользователя сообщение об ошибке и прогрессе, показывающее модальное всплывающее окно, я напрямую добавил его в свой компонент приложения. Surrender Pet Component -> Где я пытаюсь использовать опцию «Уведомление для показа».

Я думаю, что эти BhaviourSubjects не передают данные так, как я ожидал.

Служба уведомлений:

    import { Injectable } from '@angular/core';
    import { BehaviorSubject, Subject } from 'rxjs';

    @Injectable({
    providedIn: 'root'
    })
    export class NotificationService {
       successMessageSubject = new BehaviorSubject<string | null>(null);
       errorMessageSubject = new BehaviorSubject<string | null>(null);

       successMessageAction$ = this.successMessageSubject.asObservable();
       errorMessageAction$ = this.errorMessageSubject.asObservable();

       setSuccessMessage(message: string) {
       this.successMessageSubject.next(message);
     }

   setErrorMessage(message: string) {
   this.errorMessageSubject.next(message);
    console.info(this.errorMessageSubject.getValue());
   }

   clearSuccessMessage() {
    this.successMessageSubject.next(null);
    }

    clearErrorMessage() {
      this.errorMessageSubject.next(null);
    }

   clearAllMessages() {
    this.clearSuccessMessage();
    this.clearErrorMessage();
     }
    }

Компонент уведомления:

import { Component, OnInit, inject } from '@angular/core';
import { NotificationService } from '../../../core/services/notifiaction/notification.service';
import { AsyncPipe, CommonModule, NgIf } from '@angular/common';
import { tap } from 'rxjs';

@Component({
  selector: 'app-notification',
  standalone: true,
  imports: [NgIf,AsyncPipe,CommonModule],
  templateUrl: './notification.component.html',
  styleUrl: './notification.component.scss',
 
})
export class NotificationComponent implements OnInit {
   
   private notificationService:NotificationService = inject(NotificationService);
   
  
   successMessage$ = this.notificationService.successMessageAction$.pipe(
     tap((message)=>{
      if (message){
      console.info('clicked')
      setTimeout(()=>{
        this.notificationService.clearAllMessages()  
      },5000)
    }
     })
   )

   errorMessage$ = this.notificationService.errorMessageAction$.pipe(
    tap((message)=>{
      console.info(message);
      if (message){
     console.info('clicked')
     setTimeout(()=>{
       this.notificationService.clearAllMessages()  
     },5000)
    }
    })
  )


   ngOnInit(): void {
    console.info("initialized")
  }
  }

Сдать компонент питомца

import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
import { RouterLink } from '@angular/router';
import { ButtonComponent } from '../../../shared/components/button/button.component';
import { NgClass, NgIf, } from '@angular/common';
import { Component, inject } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { PetsAdopteService } from '../../../core/services/pets-adopte/pets-adopte.service';
import { SurrenderPet } from '../../../core/models/surrenderPet.model';
import { NotificationService } from '../../../core/services/notifiaction/notification.service';

@Component({
  selector: 'app-surrender-pet',
  standalone: true,
  imports: [
    ReactiveFormsModule,
    NgClass,
    RouterLink,
    NgIf,
    ButtonComponent,
    HttpClientModule
  ],
  providers:[PetsAdopteService,NotificationService],
  templateUrl: './surrender-pet.component.html',
  styleUrl: './surrender-pet.component.scss'
})
export class SurrenderPetComponent {

  private petAdopteService=inject(PetsAdopteService);
  private notificationService=inject(NotificationService);

  submitted:boolean = false;

  registerPet= new FormGroup({
    name: new FormControl<string>('',[Validators.required]),
    phoneNo:new FormControl<string>('',[Validators.required]),
    petType:new FormControl<string> ('',[Validators.required]),
    location:new FormControl<string>('',[Validators.required]),
    otherDetails:new FormControl<string>('',[Validators.required])
  })

  onSubmit(){
      this.submitted = true;
      if (this.registerPet.valid){
       this.petAdopteService.sendPetSurrender_Request(this.registerPet.value as SurrenderPet).subscribe(
        {
          next:(data)=>{
         console.info(data);
        }
         
        }
       )
      }
  }
}

HTML-шаблон компонента уведомления:

<div class = "z-40">
   <div class = "z-40" *ngIf = "successMessage$ | async as successMessage" id = "toast-success" class = " fixed fixed bottom-5 right-5 flex items-center w-full max-w-xs p-4 mb-4 text-gray-500 bg-white rounded-lg shadow dark:text-gray-400 dark:bg-gray-800" role = "alert">
       <div class = "inline-flex items-center justify-center flex-shrink-0 w-8 h-8 text-green-500 bg-green-100 rounded-lg dark:bg-green-800 dark:text-green-200">
           <svg class = "w-5 h-5" aria-hidden = "true" xmlns = "http://www.w3.org/2000/svg" fill = "currentColor" viewBox = "0 0 20 20">
               <path d = "M10 .5a9.5 9.5 0 1 0 9.5 9.5A9.51 9.51 0 0 0 10 .5Zm3.707 8.207-4 4a1 1 0 0 1-1.414 0l-2-2a1 1 0 0 1 1.414-1.414L9 10.586l3.293-3.293a1 1 0 0 1 1.414 1.414Z"/>
           </svg>
           <span class = "sr-only">Success icon</span>
       </div>
       <div class = "ms-3 text-sm font-normal">{{successMessage}}</div>
       <button type = "button" class = "ms-auto -mx-1.5 -my-1.5 bg-white text-gray-400 hover:text-gray-900 rounded-lg focus:ring-2 focus:ring-gray-300 p-1.5 hover:bg-gray-100 inline-flex items-center justify-center h-8 w-8 dark:text-gray-500 dark:hover:text-white dark:bg-gray-800 dark:hover:bg-gray-700" data-dismiss-target = "#toast-success" aria-label = "Close">
           <span class = "sr-only">Close</span>
           <svg class = "w-3 h-3" aria-hidden = "true" xmlns = "http://www.w3.org/2000/svg" fill = "none" viewBox = "0 0 14 14">
               <path stroke = "currentColor" stroke-linecap = "round" stroke-linejoin = "round" stroke-width = "2" d = "m1 1 6 6m0 0 6 6M7 7l6-6M7 7l-6 6"/>
           </svg>
       </button>
   </div>

   <div *ngIf = "errorMessage$ | async as errorMessage" id = "toast-failure" class = " fixed fixed bottom-5 right-5 flex items-center w-full max-w-xs p-4 mb-4 text-gray-500 bg-white rounded-lg shadow dark:text-gray-400 dark:bg-gray-800" role = "alert">
       <div class = "inline-flex items-center justify-center flex-shrink-0 w-8 h-8 text-green-500 bg-green-100 rounded-lg dark:bg-green-800 dark:text-green-200">
           <svg class = "w-5 h-5" aria-hidden = "true" xmlns = "http://www.w3.org/2000/svg" fill = "currentColor" viewBox = "0 0 20 20">
               <path d = "M10 .5a9.5 9.5 0 1 0 9.5 9.5A9.51 9.51 0 0 0 10 .5Zm3.707 8.207-4 4a1 1 0 0 1-1.414 0l-2-2a1 1 0 0 1 1.414-1.414L9 10.586l3.293-3.293a1 1 0 0 1 1.414 1.414Z"/>
           </svg>
           <span class = "sr-only">Success icon</span>
       </div>
       <div class = "ms-3 text-sm font-normal">{{errorMessage}}</div>
       <button type = "button" class = "ms-auto -mx-1.5 -my-1.5 bg-white text-gray-400 hover:text-gray-900 rounded-lg focus:ring-2 focus:ring-gray-300 p-1.5 hover:bg-gray-100 inline-flex items-center justify-center h-8 w-8 dark:text-gray-500 dark:hover:text-white dark:bg-gray-800 dark:hover:bg-gray-700" data-dismiss-target = "#toast-success" aria-label = "Close">
           <span class = "sr-only">Close</span>
           <svg class = "w-3 h-3" aria-hidden = "true" xmlns = "http://www.w3.org/2000/svg" fill = "none" viewBox = "0 0 14 14">
               <path stroke = "currentColor" stroke-linecap = "round" stroke-linejoin = "round" stroke-width = "2" d = "m1 1 6 6m0 0 6 6M7 7l6-6M7 7l-6 6"/>
           </svg>
       </button>
   </div>
</div>
   


PetsAdopteService, где я вызывал функции setMessages!


    import { HttpClient } from '@angular/common/http';
    import { Injectable, inject } from '@angular/core';
    import { EMPTY, Observable, catchError, tap } from 'rxjs';
    import { SurrenderPet } from '../../models/surrenderPet.model';
    import { environment } from '../../../../environments/environment.development';
    import { petsSurrenderEndpoints } from '../../constants/APIEndPoints/petsAdopte.EndPoints';
    import { NotificationService } from '../notifiaction/notification.service';

    @Injectable({
      providedIn: 'root'
    })
    export class PetsAdopteService {

    constructor() { }

    private http:HttpClient=inject(HttpClient);
    private notificationService=inject(NotificationService);

    sendPetSurrender_Request(payload:SurrenderPet):Observable<SurrenderPet>{
     return this.http.post<SurrenderPet>                                                                              (environment.apiUrl+petsSurrenderEndpoints?.createSurrenderRequest,payload).pipe(
        tap((data)=>{
        this.notificationService.setSuccessMessage('Your request sent successfully')
      }),
      catchError((error)=>{
        console.info(error)
         this.notificationService.setErrorMessage("eROR");
         return EMPTY;
      })
     )
     }
    }


Есть ли способ решить эту проблему, не выбирая сигнал, потому что я хочу узнать больше о rxjs!

«Я думаю, что эти BehaviorSubject не выдают данные так, как я ожидал» — уточните, пожалуйста. Каково фактическое поведение и какое ожидаемое поведение?

mbojko 09.05.2024 15:35

Например, когда форма отправлена ​​со стороны клиента, но с сервера получена ошибка 500, необходимо вызвать функцию ClearErrorMessage(), тогда компонент уведомления должен показать удобную для пользователя ошибку.

Berlin Johns. M 09.05.2024 15:51

@BerlinJohns.M, пожалуйста, поделитесь HTML-кодом компонента уведомлений, если возможно, стекблицем!

Naren Murali 09.05.2024 16:43

Я добавил в вопрос html-шаблон компонента уведомления, надеюсь, это будет полезно для отладки!

Berlin Johns. M 10.05.2024 11:18

может быть, ты сможешь создать стекблиз

huan feng 10.05.2024 11:20
Тестирование функциональных ngrx-эффектов в Angular 16 с помощью Jest
В системе управления состояниями ngrx, совместимой с Angular 16, появились функциональные эффекты. Это здорово и делает код определенно легче для...
Angular и React для вашего проекта веб-разработки?
Angular и React для вашего проекта веб-разработки?
Когда дело доходит до веб-разработки, выбор правильного front-end фреймворка имеет решающее значение. Angular и React - два самых популярных...
Эпизод 23/17: Twitter Space о будущем Angular, Tiny Conf
Эпизод 23/17: Twitter Space о будущем Angular, Tiny Conf
Мы провели Twitter Space, обсудив несколько проблем, связанных с последними дополнениями в Angular. Также прошла Angular Tiny Conf с 25 докладами.
Угловой продивер
Угловой продивер
Оригинал этой статьи на турецком языке. ChatGPT используется только для перевода на английский язык.
Мое недавнее углубление в Angular
Мое недавнее углубление в Angular
Недавно я провел некоторое время, изучая фреймворк Angular, и я хотел поделиться своим опытом со всеми вами. Как человек, который любит глубоко...
Освоение Observables и Subjects в Rxjs:
Освоение Observables и Subjects в Rxjs:
Давайте начнем с основ и постепенно перейдем к более продвинутым концепциям в RxJS в Angular
0
5
93
1
Перейти к ответу Данный вопрос помечен как решенный

Ответы 1

Ответ принят как подходящий

Хорошо, я наконец нашел решение. Такое странное поведение произошло из-за того, что я добавил службы в массив поставщиков автономных компонентов. Они уже были предусмотрены на корневом уровне. После того как я их удалил, логика заработала правильно.

Другие вопросы по теме