typescript 无法处理动画-返回零个元素

cld4siwp  于 2022-11-30  发布在  TypeScript
关注(0)|答案(2)|浏览(142)

当我为我的数据创建了一个服务后,动画就停止工作了。看起来这是因为当页面最初加载时,ng-repeat中没有元素(因为它正在获取数据)。看起来与THISTHIS的情况类似:
错误:

ERROR Error: Unable to process animations due to the following failed trigger transitions @listAnimation has failed due to:

- `query(":enter")` returned zero elements. (Use `query(":enter", { optional: true })` if you wish to allow this.)

动画:

trigger('listAnimation', [
      transition('* => *', [
        query(':enter', style({ opacity: 0}), { optional: true }),
        query(':enter', stagger('150ms', [
          animate('.5s ease-in', keyframes([
            style({ opacity: 0, /*transform: 'translateY(-75px)',*/ offest: 0}),
            style({ opacity: .5, /*transform: 'translateY(35px)',*/ offest: .3}),
            style({ opacity: 1, /*transform: 'translateY(0px)',*/ offest: 1})
          ]))
        ]))
      ])
    ])

于飞:

<div [@listAnimation]>
    <div *ngFor="let member of members" class="member-card">
      member info goes here...
    </div>
</div>

现在,在您说"put in { optional: true } like the error message says"之前......我已经这样做了-它消除了错误消息,但实际上并没有使动画工作

trigger('listAnimation', [
      transition('* => *', [
        query(':enter', style({ opacity: 0}), { optional: true }),
        query(':enter', stagger('150ms', [
          animate('.5s ease-in', keyframes([
            style({ opacity: 0, /*transform: 'translateY(-75px)',*/ offest: 0}),
            style({ opacity: .5, /*transform: 'translateY(35px)',*/ offest: .3}),
            style({ opacity: 1, /*transform: 'translateY(0px)',*/ offest: 1})
          ]))
        ]), {optional: true})
      ])
    ])

请记住,在将数据从应用程序组件中取出之前,这一切都是有效的。数据在那里,应用程序功能正常,只是这个动画不再工作了。

az31mfrm

az31mfrm1#

确保只有当成员数组不为空时才显示列表容器,这样只有当列表中有内容时才会触发动画。

<div [@listAnimation] *ngIf="members.length > 0">
    <div *ngFor="let member of members" class="member-card">
      member info goes here...
    </div>
</div>
jv4diomz

jv4diomz2#

trigger('routeAnimations', [
  transition('* <=> *', [
    style({ position: 'relative' }),
    query(':enter, :leave', [
      style({
        position: 'absolute',
        top: 0,
        left: 0,
        width: '100%',
      }),
    ]),
    query(':enter', [style({ left: '-100%' })]),
    query(':leave', animateChild(), { optional: true }),  //adding here solved my problem
    group([
      query(':leave', [
        animate('300ms ease-out', style({ left: '100%', opacity: 0 })),
      ]),
      query(':enter', [animate('300ms ease-out', style({ left: '0%' }))]),
    ]),
  ]),
])

相关问题