[英]How to trigger animation when routed to the same page with different URL parameter using Angular in Ionic 5
I was trying to to trigger the animation when the page is routed to itself with different url parameter.当页面使用不同的 url 参数路由到自身时,我试图触发 animation 。
For instance, for post/1
url, the animation works fine but if I route to post/2
or post/3
, the animation does not work.例如,对于
post/1
url,animation 工作正常,但如果我路由到post/2
或post/3
,animation 不起作用。
I wrote the animation using Ionic Animation and calling the method everytime the route parameter changes.我使用离子 Animation编写了 animation 并在每次路由参数更改时调用该方法。 Could anyone please help?
有人可以帮忙吗?
Here's an excerpt from my code这是我的代码的摘录
HTML HTML
<ion-icon class="custom-icon" name="chevron-back-outline"></ion-icon>
TS TS
constructor(private animationController: AnimationController) {
this.route.params.subscribe((val) => {
this.animateIcon();
});
}
animateIcon() {
this.animationController
.create()
.addElement(document.querySelector('.custom-icon'))
.duration(1500)
.iterations(3)
.fromTo('transform', 'translateX(0px)', 'translateX(-80px)')
.fromTo('opacity', '1', '0')
.play();
}
Try to Put calling function to ionViewWillEnter()尝试将调用 function 到 ionViewWillEnter()
constructor(private animationController: AnimationController) {
}
ionViewWillEnter(){
this.route.params.subscribe((val) => {
this.animateIcon();
});
}
animateIcon() {
this.animationController
.create()
.addElement(document.querySelector('.custom-icon'))
.duration(1500)
.iterations(3)
.fromTo('transform', 'translateX(0px)', 'translateX(-80px)')
.fromTo('opacity', '1', '0')
.play();
}
I have figured it out myself.我自己已经弄清楚了。 Instead of capturing the class name using
querySelector
, I used ElementRef
notation to make it work.我没有使用
querySelector
捕获 class 名称,而是使用ElementRef
表示法使其工作。 Now it does work all the time when I route to any of the /post
pages.现在,当我路由到任何
/post
页面时,它一直都在工作。
HTML HTML
<ion-icon #leftIcon class="custom-icon" name="chevron-back-outline"></ion-icon>
TS TS
@ViewChild('icon1', { read: ElementRef, static: false }) leftIcon: ElementRef;
constructor(private animationController: AnimationController) {
this.route.params.subscribe((val) => {
this.animateIcon();
});
}
animateIcon() {
this.animationController
.create()
.addElement(this.leftIcon.nativeElement)
.duration(1500)
.iterations(3)
.fromTo('transform', 'translateX(0px)', 'translateX(-80px)')
.fromTo('opacity', '1', '0')
.play();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.