角度路线过渡 - 根据当前路线向左或向右滑动?

xDr*_*ago 4 animation angular-routing angular angular-animations

我按照教程添加了路线过渡动画,它似乎有效,但我的应用程序中有超过 3 条路线。现在,当我转到animation: 'isRight'当前所在的页面时,isLeft它按预期工作,但是当我已经在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' } },
];

Run Code Online (Sandbox Code Playgroud)

那是我的动画:

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()),
    ];
}
Run Code Online (Sandbox Code Playgroud)

aka*_*ash 6

使用: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} },
];
Run Code Online (Sandbox Code Playgroud)

那么你transitions应该像 -

trigger('routeAnimations', [
    transition(':increment', slideTo('right') ),
    transition(':decrement', slideTo('left') ),
  ]);
Run Code Online (Sandbox Code Playgroud)

看看这个 stackblitz 例子。有关所有详细信息,另请参阅此 angular 文档