gpt4 book ai didi

javascript - 在 Nativescript Angular 中动画后添加新项目到集合时 ngFor 崩溃

转载 作者:行者123 更新时间:2023-12-02 22:14:26 24 4
gpt4 key购买 nike

我使用 ngFor 将 View 放置在另一个 View 上。卡牌种类:

<GridLayout rows="*">
<GridLayout row="0" *ngFor="let image of images; trackBy: trackByList"
verticalAlignment="center" horizontalAlignment="center"
[width]="width" [height]="width" (loaded)="itemLoaded($event)"
[rotate]="getRotationAngle(image)" [style.z-index]="image.zIndex">
<Image [src]="image.path" [id]="image.zIndex" stretch="aspectFill"
[width]="width" [height]="width">
</Image>
</GridLayout>
</GridLayout>


itemLoaded(event) {
let grid: any = event.object;

grid.on(GestureTypes.swipe, (args: SwipeGestureEventData) => {
if (this.animationIsRunning) {
return;
}
let x = this.width * 1.5;
let rotate = 45;

if (args.direction == SwipeDirection.right) { }
else if (args.direction == SwipeDirection.left) {
x *= -2;
rotate *= -1;
}
else {
return;
}


this.animationIsRunning = true;
grid.animate({ translate: { x: x, y: 0 }, opacity: 0, rotate: rotate, duration: 500 })
.then(() => {
setTimeout(() => {
this.swipeFinished();
}, 10);
})
.catch((e) => {
console.log(e.message);
})
})
}
//
private swipeFinished() {
this.swipedImages++;

if (this.images.length - this.swipedImages <= 2) {

console.log("Requesting next page");
this.addNextPage();
console.log("Next page loaded. Refreshing changes.");
this.cdr.detectChanges();

this.animationIsRunning = false;
}
else {
this.animationIsRunning = false;
}
}
trackByList(index, item: ImageObj) {
return item.zIndex;
}

我正在使用页面机制从服务器加载项目。每次5个。加载下一页并将新元素插入到 items 并调用 this.cdr.detectChanges() 后,我崩溃了”

Unhandled Promise rejection: Cannot read property 'destroyed' of null ; Zone: ; Task: Promise.then ; Value: TypeError: Cannot read property 'destroyed' of null

  • 我正在使用 ChangeDetectionStrategy OnPush
  • 看起来这是由于滑动动画造成的

Playground example (Need to do swipes in order to trigger reload and crash)

最佳答案

查看您的代码后,我发现崩溃的两个主要原因。

  • 您正在 Angular 上下文之外运行滑动事件,实际上您正在手动触发更改检测,但我怀疑它是否在正确的时间完成。
  • 此外,您没有在动画后清理数组,因此随着时间的推移,可能会有数百张图像最终也会影响您的性能。

这是Updated Playground ,崩溃似乎已在我这边解决了。但老实说,它仍然可以改进。我建议您查看 Tinder Style Cards with NativeScript Angular 上的博客文章作者:Jen Looper,这可能会帮助您改进设计。

关于javascript - 在 Nativescript Angular 中动画后添加新项目到集合时 ngFor 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59437007/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com