“désabonnement angulaire dans le service” Réponses codées

désabonnement angulaire de l'observable

//Please note that there are many ways to unsubscribe, this is one of them
import { Subscription } from 'rxjs';

private searchEventSubscription: Subscription;

export class someComponent OnInit, OnDestroy {

    constructor(private someService: SomeService) { }

    ngOnInit() {
      this.searchEventSubscription = this.someService.someMethod.subscribe(result => {  
        doSomething(result);
      });
    }

    ngOnDestroy() {
		this.searchEventSubscription.unsubscribe()
    }
ChernobylBob

Comment se désabonner dans le service angulaire

@Component({...})
export class AppComponent implements OnInit, OnDestroy {
    subscription: Subscription 
    ngOnInit () {
        var observable = Rx.Observable.interval(1000);
        this.subscription = observable.subscribe(x => console.log(x));
    }
    ngOnDestroy() {
        this.subscription.unsubscribe()
    }
}
Farwa Miraj

Angular abonnez-vous et se désabonnez

import { Component, OnDestroy, OnInit } from '@angular/core';
// RxJs 6.x+ import paths
import { filter, startWith, takeUntil } from 'rxjs/operators';
import { Subject } from 'rxjs';
import { BookService } from '../books.service';

@Component({
    selector: 'app-books',
    templateUrl: './books.component.html'
})
export class BooksComponent implements OnDestroy, OnInit {
    private ngUnsubscribe = new Subject();

    constructor(private booksService: BookService) { }

    ngOnInit() {
        this.booksService.getBooks()
            .pipe(
               startWith([]),
               filter(books => books.length > 0),
               takeUntil(this.ngUnsubscribe)
            )
            .subscribe(books => console.log(books));

        this.booksService.getArchivedBooks()
            .pipe(takeUntil(this.ngUnsubscribe))
            .subscribe(archivedBooks => console.log(archivedBooks));
    }

    ngOnDestroy() {
        this.ngUnsubscribe.next();
        this.ngUnsubscribe.complete();
    }
}
Repulsive Rabbit

désabonnement angulaire dans le service

@Component({...})
export class AppComponent implements OnInit, OnDestroy {
    subscription1$: Subscription
    subscription2$: Subscription 
    ngOnInit () {
        var observable1$ = Rx.Observable.interval(1000);
        var observable2$ = Rx.Observable.interval(400);
        this.subscription1$ = observable.subscribe(x => console.log("From interval 1000" x));
        this.subscription2$ = observable.subscribe(x => console.log("From interval 400" x));
    }
    ngOnDestroy() {
        this.subscription1$.unsubscribe()
        this.subscription2$.unsubscribe()
    }
}
Prabha karan

Réponses similaires à “désabonnement angulaire dans le service”

Questions similaires à “désabonnement angulaire dans le service”

Plus de réponses similaires à “désabonnement angulaire dans le service” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code