Angular: transition between states isn't animated correctly if animate() is placed inside the group()

旧城冷巷雨未停 提交于 2019-12-23 22:14:08

问题


I have a parent component with a nested child component, both have :enter and :leave animations. To run both animations in parallel I use group() and animateChild() functions. But, if the parent animate() call is located inside group() call, the parent state transition isn't animated.

animations: [
    trigger('child', [
        state('void', style({
            opacity: 0
        })),
        transition('void <=> *', animate('1750ms ease-in'))
    ]),
    trigger('parent', [
        state('void', style({
            height: 0
        })),
        transition('void <=> *', group([
            animate('1750ms ease-out'),
            query('@child', [
                animateChild()
            ])
        ]))
    ])
]

Because of this, I must describe void => * and * => void parent transitions separately, and move parent style description into the transitions. But in this case the parent animation description is much more complicated:

trigger('parent', [
    transition('void => *', [
        style({
            height: 0
        }),
        group([
            animate('1750ms ease-in', style({
                height: '*'
            })),
            query('@child', [
                animateChild()
            ])
        ])
    ]),
    transition('* => void', [
        style({
            height: '*'
        }),
        group([
            animate('1750ms ease-in', style({
                height: 0
            })),
            query('@child', [
                animateChild()
            ])
        ])
    ])
])

UPDATE:

The workaround above can be improved by moving the parent animation into a separate animation:

const parentAnimation = animation([
    style({
        height: '{{startHeight}}'
    }),
    group([
        animate('175ms ease-in', style({
            height: '{{endHeight}}'
        })),
        query('@child', [
            animateChild()
        ])
    ])
]);

@Component({
    animations: [
        trigger('parent', [
            transition('void => *', [
                useAnimation(parentAnimation, {
                    params: {
                        startHeight: 0,
                        endHeight: '*'
                    }
                })
            ]),
            transition('* => void', [
                useAnimation(parentAnimation, {
                    params: {
                        startHeight: '*',
                        endHeight: 0
                    }
                })
            ])
        ])
    ]
})

回答1:


I found a workaround! Try swapping the query() and animate(). I think it's a bug, not sure.

I have reported this issue: https://github.com/angular/angular/issues/28654



来源:https://stackoverflow.com/questions/51726044/angular-transition-between-states-isnt-animated-correctly-if-animate-is-plac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!