2017-05-23 2 views
0

Salut Je suis en train de tester comment utiliser un minuteur. Utilisez un abonnement pour exécuter une fonction dans un laps de temps. Le problème que je rencontre est qu'une fois que je me désinscris et que je change de composant, la méthode consiste à m'appeler. Je voudrais savoir comment l'arrêter. voici mon code:Angulaire 2 MVC S'abonner à la fonction avec la minuterie qui appelle toujours la méthode après la désinscription

`

import { Component, OnInit, AfterViewChecked, AfterContentInit, AfterViewInit, OnDestroy } from '@angular/core'; 
import $ from 'jquery'; 
import { Headers, Http } from "@angular/http"; 
import "rxjs/Rx"; 
import { Observable } from 'rxjs/Observable'; 
import { Subscription } from 'rxjs/Subscription'; 

// Variables that load the respectively JavasScript methods in the JavaScript file SharedContents/plugins/flot-charts/jquery.flot.js 
declare var initRealTimeChart: any; 
declare var initSparkline: any; 
declare var initDonutChart: any; 

@Component({ 
    selector: 'home', 
    template: require('./home.component.html') 
}) 

export class HomeComponent implements OnInit, AfterViewChecked, AfterContentInit, AfterViewInit, OnDestroy { 

    _message: string; 
    foo; 
    private subscriptions: Array<Subscription> = []; 

    //called first time before the ngOnInit() 
    constructor(private http: Http) {} 

    //called after the constructor and called after the first ngOnChanges() 
    ngOnInit(): void { 
     this._message = "adsad"; 
     console.log(this._message); 
     //setInterval(() => this.getLog("Home/GetTest"), 500); 

     this.subscriptions.push(this.getLog("Home/GetTest").subscribe(
      response => { 
       console.log('Success ' + response); 
      }, 
      err => { 
       console.log('Error' + err) 
      }, 
      () => { 
       console.log('Complete') 
      } 
     )); 


    } 

    ngAfterContentInit(): void { 

    } 

    ngAfterViewChecked(): void { 

    } 

    ngAfterViewInit(): void { 
     initRealTimeChart(); 
     initSparkline(); 
     initDonutChart(); 
    } 

    ngOnDestroy(): void { 

     console.log("ngOnDestroy!"); 

     this.subscriptions.forEach((subscription: Subscription) => { 
      console.log("Destroying Subscription :", subscription); 
      subscription.unsubscribe(); 
     }); 
    } 

    public TestStuff() { 

     console.log("Run TestTSuff"); 

     return this.http.get("Home/GetTest") 
      .toPromise() 
      .then(response => { 
       response.json().data; 
       console.log('GOT IT!'); 
       console.log(response); 
      }) 
      .catch((ex) => { 
       console.log(ex); 
       console.log(ex._body); 
      }); 
    } 

    getLog(url: string): Observable<string> { 
     return Observable 
      .timer(0, 12000) 
      .concatMap(() => this.get(url)) 
      ///.retryWhen(error => error.delay(12000)) 
      .map((data: any) => { 
       console.log('Request Success!') 
       data.json(); 
       return data.json(); 
      }) 
      .catch(e => { 
       //console.warn(e.toString()); 
       return Observable.from("ERROR" + e); 
      }); 
    } 

    get(url) { 
     return this.http.get(url); 
    } 
} 

`

Répondre

0

j'ai réussi à résoudre ce problème.

Tout ce dont j'avais besoin était de vider le cache de navigation.

Merci pour toute l'aide de toute façon.