“Angular cancelar la suscripción en servicio” Código de respuesta

Angular cancelar la suscripción de 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

cómo darse de baja en el servicio angular

@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 suscribir y cancelar la suscripción

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

Angular cancelar la suscripción en servicio

@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

Respuestas similares a “Angular cancelar la suscripción en servicio”

Preguntas similares a “Angular cancelar la suscripción en servicio”

Más respuestas relacionadas con “Angular cancelar la suscripción en servicio” en JavaScript

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código