简体   繁体   English

IONIC - ANGULAR 为什么单击返回按钮时我有相同的多个请求?

[英]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.

相关问题 单击后退按钮,Ionic Angular 应用程序将进入登录屏幕 - Ionic Angular app going to login screen on click of back button 使用Ionic 2在iOS上单击“后退”按钮时,如何阻止导航栏消失? - How do I stop the navbar from disappearing when I click the back button on iOS using Ionic 2? Angular 离子后退按钮只有在我在它上面滑动指针后才能工作 - Angular Ionic back button only working after I slide the pointer on It 关闭角模态并在单击后退按钮时保持在同一页面上 - Close angular modal and remain on same page on back button click 当我在 Angular 中单击后退按钮时,我无法导航到索引页面 - I cant navigate to the index page, when i click back button in Angular 当我们使用角度和离子单击按钮时,如何获取ID? - how to get id when we are click the button using angular and ionic? 离子角度 - 当我有多个嵌套路由时,路由无法正常工作 - Ionic-angular - Routing is not working properly when I have multiple nested routes 使用Ionic2 / angular2的设备(Android)后退按钮单击检测 - Device(Android) back button click detection using Ionic2/angular2 使用 Ionic (angular)。无法访问在显示屏/屏幕中有按钮的移动设备的硬件后退按钮 - Using Ionic (angular).Unable to access Hardware back button of mobile devices which have buttons inside the display/screen 当我使用 angular 单击按钮时如何拥有所有子类别 - how to have all the subcategories when I click on the button all using angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM