【发布时间】:2017-07-12 17:58:40
【问题描述】:
我有一个大型 Angular2 应用程序。我有一条路线是/rewards,这是我们提供的所有奖励的列表。我拥有的另一条路线是/reward/{rewardName},这是一个单一的奖励详情页面。我遇到的问题是,当我在/rewards 路由和不同的/reward/{rewardName} 路由之间切换回来和第四次时,我点击的所有/reward/{rewardName} 页面继续运行。我知道这一点是因为我的ngOnInit 在我的/reward/{rewardName} 组件中有一个setInterval。因此,如果我点击/reward/{rewardName} 路由 5 次,它将注销 5 个quantity 实例,如下面的代码所示。为什么这个组件永远不会停止运行? (无论我在站点的其余部分转到哪个页面,都会继续运行)
代码:
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)
}
...
}
【问题讨论】:
标签: angular