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辅助软件的真假(透视)海贝之城有辅助吗(其实是有辅助插件)-哔哩哔哩1、用...
今天下午!德州透视插件(透视)... 今天下午!德州透视插件(透视)樱花之盛挂(竟然存在有辅助辅助器)-哔哩哔哩樱花之盛挂脚本下载中分为三...
透视好友!wepokerh5破... 透视好友!wepokerh5破解(透视)微乐河南小程序微乐辅助脚本(好像存在有辅助软件)-哔哩哔哩1...
透视神器!we-poker辅助... 透视神器!we-poker辅助软件教程(透视)新广西老友辅助(切实是有辅助下载)-哔哩哔哩新广西老友...
透视总结!哈糖大菠萝能开挂吗(... 透视总结!哈糖大菠萝能开挂吗(透视)多乐破解游戏盒子破解版(都是有辅助软件)-哔哩哔哩1、多乐破解游...
围绕透视问题!aapoker能... 您好,南通长牌有挂吗这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩家...
据目击者称!wepoker代打... 据目击者称!wepoker代打辅助(透视)微信闲来辅助神器免费(果然存在有辅助插件)-哔哩哔哩小薇(...
近日!hhpoker透视脚本视... 近日!hhpoker透视脚本视频(透视)微乐卡五星祈福有用吗(好像真的是有辅助平台)-哔哩哔哩1、玩...
透视好友!wepoker透视有... 透视好友!wepoker透视有用吗(透视)拱趴大菠萝9人场技巧(真是真的是有辅助插件)-哔哩哔哩1、...
现场直击!如何下载德普之星辅助... 现场直击!如何下载德普之星辅助软件(透视)吉祥填大坑底牌图片(好像真的有辅助下载)-哔哩哔哩1、点击...