当我再次回到同一页面时,数据被复制

时间:2019-05-12 16:23:10

标签: angular ionic-framework ionic4 lifecycle

我正在使用ionViewWillEnterionViewWillLeave作为我的离子项目页面的生命周期挂钩,但是在某些页面中,如果我导航到另一个页面并返回到该页面,则数据将被复制。它将在ionViewWillEnter中获取的新数据添加到旧数据中。

我需要做的是设置ionViewWillLeave的正确方法,以确保所有旧的提取数据都消失了。

这是我的代码:

import { Component, OnInit, OnDestroy } from '@angular/core';
import { LanguageService } from '../services/language.service';
import { Dress, DressService } from '../services/dress.service';
import { AuthService } from '../services/auth.service';

@Component({
  selector: 'app-my-ads',
  templateUrl: './my-ads.page.html',
  styleUrls: ['./my-ads.page.scss'],
})
export class MyAdsPage {
  allDress: Dress[];
  theSub: any;
  currentEmail = '';

  constructor(
    private currentLanguage: LanguageService,
    private dressService: DressService,
    private authService: AuthService,
  ) {
    this.currentEmail = this.authService.userDetails().email;
  }

  ionViewWillEnter() {
    this.theSub = this.dressService.getDressesForMyAds().subscribe(res => {
      this.allDress = res;
    });
    console.log('myAds started');
  }
  ionViewWillLeave() {
    this.theSub.unsubscribe();
    console.log('myAds leaved');
  }

  getCurrentLanguage() {
    return this.currentLanguage.getCurrentLanguage();
  }

  changeToArabic() {
    this.currentLanguage.changeToArabic();
  }

  changeToEnglish() {
    this.currentLanguage.changeToEnglish();
  }

}

1 个答案:

答案 0 :(得分:0)

我必须删除ionViewWillLeave并对此进行更改:

  ngOnDestroy() {
    this.theSub.unsubscribe();
    console.log('myAds leaved');
    // tslint:disable-next-line: forin
    for (const member in this.allDress) { delete this.allDress[member]; }
  }

删除之前删除获取的数据将其全部修复。 for (const member in this.allDress) { delete this.allDress[member]; }