Angular 路线转换 - 根据当前路线向左或向右滑动?

Angular Route Transition - Slide left or right dependent on current route?

我按照 this 教程添加了路线转换动画,它似乎有效,但我的应用程序中有超过 3 条路线。现在,当我在 isLeft 上使用 animation: 'isRight' 访问一个页面时,它按预期工作,但是当我已经在 isRight 上并想转到另一个页面时当前的右侧,它根本没有显示任何转换。

如何根据我目前所在的位置进行转换?我怎么知道我是否必须向左或向右过渡?

这是我的路线示例:

const routes: Routes = [
  { path: 'page1', component: Page1Component, data: {animation: isLeft} },
  { path: 'page2', component: Page2Component, data: { animation: 'isRight' } },
  { path: 'page3', component: Page3Component, data: { animation: 'isRight' } },
  { path: 'page4', component: Page4Component, data: { animation: 'isRight' } },
  { path: 'page5', component: Page5Component, data: { animation: 'isRight' } },
  { path: 'page6', component: Page6Component, data: { animation: 'isRight' } },
];

那是我的动画:

export const slider =
    trigger('routeAnimations', [
        transition('* => isLeft', slideTo('left')),
        transition('* => isRight', slideTo('right')),
        transition('isRight => *', slideTo('left')),
        transition('isLeft => *', slideTo('right'))
    ]);

function slideTo(direction) {
    const optional = { optional: true };
    return [
        query(':enter, :leave', [
            style({
                position: 'absolute',
                top: 0,
                [direction]: 0,
                width: '100%'
            })
        ], optional),
        query(':enter', [
            style({ [direction]: '-100%'})
        ]),
        group([
            query(':leave', [
                animate('600ms ease', style({ [direction]: '100%'}))
            ], optional),
            query(':enter', [
                animate('600ms ease', style({ [direction]: '0%'}))
            ])
        ]),
        // Normalize the page style... Might not be necessary

        // Required only if you have child animations on the page
        query(':leave', animateChild()),
        query(':enter', animateChild()),
    ];
}

使用 :increment:decrement 转换选择器值很容易做到。您可以定义路线,每条路线都有 animation 属性 数据集,按递增顺序设置为数值,如下所示 -

const routes: Routes = [
  { path: 'one', component: CompOneComponent, data: { animation: 0 } },
  { path: 'two', component: CompTwoComponent, data: { animation: 1 } },
  { path: 'three', component: CompThreeComponent, data: { animation: 2 } },
  { path: 'four', component: CompFourComponent, data: { animation: 3} },
];

那么你的 transitions 应该是 -

trigger('routeAnimations', [
    transition(':increment', slideTo('right') ),
    transition(':decrement', slideTo('left') ),
  ]);

查看 this stackblitz example. Please also refer to this angular document 了解所有详细信息。