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

相关内容

热门资讯

透视智能ai!wpk辅助插件,... 透视智能ai!wpk辅助插件,aapoker免费透视脚本,插件教程(都是是真的有挂);最新版2024...
透视规律!德普之星app安卓版... 1、透视规律!德普之星app安卓版破解版,wepoker有机器人吗,2025新版总结(原生存在有挂)...
透视讲解!wpk脚本辅助器,w... 透视讲解!wpk脚本辅助器,wpk插件辅助,透明挂教程(切实是有挂)是一款可以让一直输的玩家,快速成...
透视神器!hhpoker透视下... 透视神器!hhpoker透视下载链接在哪里,wepoker底牌透视脚本,AI教程(切实真的是有挂)1...
透视存在!hhpoker万能辅... 透视存在!hhpoker万能辅助器,拱趴大菠萝有挂吗,技巧教程(素来存在有挂)hhpoker万能辅助...
透视最新!aapoker辅助器... WePoke高级策略深度解析‌;透视最新!aapoker辅助器怎么用,wepoker辅助器是真的吗,...
透视科技!红龙poker脚本,... 透视科技!红龙poker脚本,wepoker正确养号方法,细节方法(本然有挂);值得一提的是,红龙p...
透视讲解!hhpoker辅助软... 透视讲解!hhpoker辅助软件下载,wpk辅助器,扑克教程(原来有挂)是由北京得hhpoker辅助...
透视真的!wepoker辅助器... 透视真的!wepoker辅助器是真的的吗,aapoker辅助软件合法吗,总结教程(切实真的有挂);最...
透视好友!wepoker辅助透... 透视好友!wepoker辅助透视软件,拱趴大菠萝有挂吗,技巧教程(切实真的是有挂);(需添加指定Q群...