繁体   English   中英

在 Nativescript Angular 动画后向集合添加新项目时,ngFor 崩溃

[英]ngFor crashes when adding new items to collection after animation in Nativescript Angular

我使用ngFor将视图放在另一个之上 卡片种类:

<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个。 加载下一页并将新元素插入项目并调用this.cdr.detectChanges() 后,我崩溃了”

未处理的承诺拒绝:无法读取 null 的属性“销毁”; 区域: ; 任务: Promise.then ; 值:类型错误:无法读取 null 的属性“已销毁”

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

游乐场示例(需要进行滑动以触发重新加载和崩溃)

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

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

这是更新的操场,崩溃似乎在我这边得到了修复。 但老实说,它仍然可以改进。 我建议您查看 Jen Looper 撰写的关于Tinder Style Cards with NativeScript Angular的博客文章,这可能会帮助您改进设计。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM