当我再次返回同一页面时数据重复
data is duplicated when i come back to the same page again
我正在使用 ionViewWillEnter
和 ionViewWillLeave
作为我的离子项目页面的生命周期挂钩,但在某些页面中,如果我导航到另一个页面并返回到它。它将 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();
}
}
我不得不删除 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]; }
我正在使用 ionViewWillEnter
和 ionViewWillLeave
作为我的离子项目页面的生命周期挂钩,但在某些页面中,如果我导航到另一个页面并返回到它。它将 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();
}
}
我不得不删除 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]; }