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中调用无法正确工作,因为组件尚未完全初始化并渲染。

相关内容

热门资讯

wpk外 挂!wpk透视辅助可... wpk外 挂!wpk透视辅助可测试真的假的(wPk)果然真的有挂-详细辅助挂教程;1、wpk系统规律...
wepoke辅助挂!wopok... 1、wepoke辅助挂!wopoker用ai有用(辅助透视)WepokE(的确是有挂的);该软件可以...
德扑之星有作弊!德扑之星实战,... 德扑之星有作弊!德扑之星实战,线上wpk德州真是是有挂(详细有辅助挂教程);人气非常高,ai更新快且...
wpk俱乐部!wpk可以提高胜... wpk俱乐部!wpk可以提高胜率,Wpk其实真的有挂,详细教程,(有挂黑科技)1、让任何用户在无需w...
德州之星辅助!德州ai辅助,德... 德州之星辅助!德州ai辅助,德州ai都是有挂(详细插件教程)1、用户打开应用后不用登录就可以直接使用...
wepoke有辅助挂!wepo... wepoke有辅助挂!wepoke辅助软件下载链接,wepoke有挂,细节揭秘(有挂详情)科技教程也...
微扑克ai辅助!wpk微扑克智... 微扑克ai辅助!wpk微扑克智能辅助(wpK)果真真的有挂-详细辅助挂教程1、全新机制【微扑克软件透...
德州AI智能辅助机器人!ai机... 德州AI智能辅助机器人!ai机器人代打德州,德州wepower都是有挂(详细ai机器人教程)1、每一...
wepoke插件!wepoke... wepoke插件!wepoke用模拟器,wepower总是真的有挂,技巧教程(有挂解说)1、玩家可以...
wpk发牌逻辑!wPK好像真的... wpk发牌逻辑!wPK好像真的有挂,WPk一般真的有挂,AI教程(有挂解密)1、wpkai辅助优化,...