Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

angular - Subject.next() not firing from inside observable

service.ts

constructor() {
    this.cartUpdate = new BehaviorSubject<Array<AppCartItem>>([]);
    // this.cartUpdate.subscribe((items) => {   // #### 1
    //   console.log('Service Items : ', items);
    // });
}
add(item) : Observable<Array<AppItem>> {
    return this.get().pipe(
      switchMap(cartItems => {
        let index = cartItems.findIndex(i => i.item.id == item.id);
        if(index == -1) {
          cartItems.push({ item });
        }
        return this.updateUser({ 'cart': cartItems })
        .pipe(
          map(() => {
            return cartItems;
          })
        )
      }),
      catchError(err => {
        console.log('Error : ', err);
        return throwError(null);
      })
    ).pipe(
      map(cartItems => {
        this.updateCart(cartItems);
        return cartItems;
      })
    )
}

updateCart(items) {
    setTimeout(() => {
      this.cartUpdate.next(items);  // #### 3
    }, 1000);
}

component.ts

ngOnInit() {
  this.service.cartUpdate.subscribe((items) => {  // ####4
    console.log('Items : ', items);
  }, (err) => {
    console.log('Error : ', err);
  });
}

I have provided the code for service and component. Initially, in the component the subscribe is triggering. No issue in that. #### 4

Now, the add method in service.ts also should trigger the observable after adding the item. #### 3

But the subscribe in component is not triggering for the emit. #### 3

Whereas the subscribe in the constructor for the same Subject is working. #### 1

I am having trouble in figuring out why the subscribe in the component is not working

question from:https://stackoverflow.com/questions/65903349/subject-next-not-firing-from-inside-observable

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Some approaches need to be corrected, Subscriptions always respond when there is a change in value. Made an example for you, follow this one

Service.ts

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

@Injectable()
export class ServiceService {
cartUpdate = new BehaviorSubject([]);
constructor() { }
add() {
    this.cartUpdate.next([1,2,1])
    this.updateCart([2, 12]);
    return this.cartUpdate.asObservable();
}
updateCart(items) {
    setTimeout(() => {
        console.log('calling');
        this.cartUpdate.next(items);
        console.log(this.cartUpdate);
    }, 3000);
}
}

component.ts

ngOnInit(): void {
this.service.add().subscribe(res =>{
  this.response = res;
  console.log('Hi'+ res);
})
}

Demo: StackBlitz


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...