Angular动画:组件在触发动画之前被销毁。
创始人
2024-10-24 14:00:54
0

一个可能的解决方法是将动画触发放在ngAfterViewInit生命周期钩子中执行,而不是ngOnInit中。这样可以确保组件已经初始化并完全渲染,不会在动画触发之前被销毁。 示例代码:

import { Component, AfterViewInit, ViewChild, ElementRef } from '@angular/core'; import { trigger, state, style, animate, transition } from '@angular/animations';

@Component({ selector: 'app-my-component', templateUrl: './my-component.component.html', styleUrls: ['./my-component.component.css'], animations: [ trigger('myAnimation', [ state('start', style({ opacity: 1 })), state('end', style({ opacity: 0 })), transition('start => end', [ animate('1s') ]) ]) ] }) export class MyComponent implements AfterViewInit { @ViewChild('myElement') myElement: ElementRef;

ngOnInit() { // This won't work because the element hasn't been rendered yet this.myElement.nativeElement.classList.toggle('myClass'); this.triggerAnimation(); }

ngAfterViewInit() { // This will work because the element has been rendered this.myElement.nativeElement.classList.toggle('myClass'); this.triggerAnimation(); }

triggerAnimation() { this.state = 'end'; } }

在这个示例中,我们在ngAfterViewInit的生命周期钩子中调用triggerAnimation()方法来触发动画。在ngOnInit中调用无法正确工作,因为组件尚未完全初始化并渲染。

相关内容

热门资讯

wepoke辅助插件,wepo... wepoke辅助插件,wepoke透视挂靠谱吗,智星德州菠萝偷偷看牌功能1、许多玩家不知道wepok...
wepoke透明挂在哪买,wp... wepoke透明挂在哪买,wpk透明挂,智星德州菠萝辅助工具1、构建自己的微扑克辅助插件;2、选择您...
德扑之星软件透明软件多少钱,w... 德扑之星软件透明软件多少钱,wepoke辅助器,智星菠萝德州辅助1、wepoke辅助器ai辅助优化,...
wepoke软件透明挂价格,w... wepoke软件透明挂价格,wpk黑科技被锤原因,智星德州菠萝怎么看有没有开挂(1)wepoke软件...
wepoke透明挂在哪买,we... wepoke透明挂在哪买,wepoke智能ai,智星德州菠萝有挂吗是一款可以让一直输的玩家,快速成为...
智星德州菠萝开挂,wepoke... 一、wepoke辅助挂简介了解软件请加微:136704302wepoke辅助挂是一款在线扑克游戏平台...
aapokerai辅助,wep... aapokerai辅助,wepoke软件透明演示,智星菠萝德州辅助1、许多玩家不知道wepoke软件...
wpk透视辅助测试,wepok... wpk透视辅助测试,wepoke辅助使用教程,智星德州菠萝辅助;wpk透视辅助测试,wepoke辅助...
智星德州菠萝辅助,wepoke... 智星德州菠萝辅助,wepokeai代打逻辑,智星德州菠萝开挂;小薇(透视辅助)致您一封信;亲爱wep...
微扑克辅助透视技能教程,wep... 您好,wepoke透明挂是真的吗这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】...