我有一個大的Angular2應用程序。我擁有的一條路線是/rewards
,這是我們提供的所有獎勵的列表。我有另一條路線是/reward/{rewardName}
這是一個單一的獎勵細節頁面。我遇到的問題是,當我再次打開和第四/rewards
路線和不同/reward/{rewardName}
路線之間,所有的,我已經打了/reward/{rewardName}
頁的繼續運行。我知道這是因爲我在/reward/{rewardName}
組件中的ngOnInit
中有setInterval
。因此,如果我點擊/reward/{rewardName}
路線5次,它將註銷quantity
的5個實例,如下面的代碼所示。爲什麼這個組件永遠不會停止運行? (繼續不管跑,我去現場的其餘哪一頁)Angular2 - 組件在更改頁面時保持運行
代碼:
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { RewardDetailService } from './reward-detail.service';
import { AuthService } from '../../auth/auth.service';
@Component({
selector: 'reward-detail-component',
styleUrls: [ './reward-detail.component.scss' ],
templateUrl: './reward-detail.component.html'
})
export class RewardDetailComponent implements OnInit {
...
quantity = 1;
constructor(private rewardsDetailService: RewardDetailService, private activatedRoute: ActivatedRoute, private authService: AuthService, private router: Router) { ... }
ngOnInit() {
setInterval(() => {
console.log('QUANTITY: ', this.quantity);
}, 1000)
}
...
}