“Angular cancelando -se em serviço” Respostas de código

angular cancelar a inscrição de observável

//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

Como cancelar a inscrição em serviço 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

Inscreva -se angular e cancelamento de inscrição

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 cancelando -se em serviço

@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

Respostas semelhantes a “Angular cancelando -se em serviço”

Perguntas semelhantes a “Angular cancelando -se em serviço”

Mais respostas relacionadas para “Angular cancelando -se em serviço” em JavaScript

Procure respostas de código populares por idioma

Procurar outros idiomas de código