[英]IONIC - ANGULAR Why when click back button I have same multiple request?
I have the back button on my pages.我的页面上有后退按钮。 When I stay on PAGE 1 and go to PAGE 2 I have only one request.当我停留在第 1 页和 go 到第 2 页时,我只有一个请求。 And when I click the back button from PAGE 2 to PAGE 1 i receive a two same request.当我从第 2 页到第 1 页单击后退按钮时,我收到两个相同的请求。 If I go to PAGE 2 again and go back I receive three same request.如果我再次 go 到 PAGE 2 并且 go 返回我收到三个相同的请求。
Why?为什么?
i have this on PAGE 1我在第 1 页上有这个
ionViewWillEnter() {
this.route.paramMap.subscribe(paramMap => {
if (!paramMap.has('id_store')) {
this.location.back();
return;
}
this.isLoading = true;
this.atv_id = paramMap.get('id_store');
this.loadingCtrl
.create({ keyboardClose: true, message: 'Carico categorie...' })
.then(loadingEl => {
loadingEl.present();
this.subcriber = this.ristoserv.postRistoCategories(this.atv_id).subscribe( (response: any ) => {
// check zona selezionata
this.storage.getObject('zoneData').then((data: any) => {
});
this.categories = response;
this.IdType = paramMap.get('id_type');
this.isLoading = false;
loadingEl.dismiss();
}, errRes => {
loadingEl.dismiss();
const code = errRes.error.error.message;
console.log('error', code);
this.isLoading = true;
});
});
});
};
Every time you call ionViewWillEnter()
you create new route subscription.每次调用ionViewWillEnter()
时,都会创建新的路由订阅。 You should unsubscribe from it.您应该取消订阅它。
Create property创建属性
private unsubscribe$ = new Subject<void>();
use it to unsubscribe用它来取消订阅
this.route.paramMap
.pipe(takeUntil(this.unsubscribe$))
.subscribe(paramMap => {
And emit it whenever component destroys.并在组件销毁时发出它。
ngOnDestroy() {
this.unsubscribe$.next();
}
You can do this easier using a library @ngneat/until-destroy .您可以使用库@ngneat/until-destroy更轻松地做到这一点。
Or simply make sure you only initialize it once: if you only change parameter in your router it might not recreate your component - depending on onSameUrlNavigation config.或者只是确保您只初始化一次:如果您只更改路由器中的参数,它可能不会重新创建您的组件 - 取决于onSameUrlNavigation配置。
It would help if you showed how/when you call ionViewWillEnter()
.如果您展示了如何/何时调用ionViewWillEnter()
将会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.