简体   繁体   English

使用 Ionic 5 中的 Angular 路由到具有不同 URL 参数的同一页面时如何触发 animation

[英]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/2post/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.

相关问题 使用 Angular 在 Ionic 5 中路由用户时如何刷新页面 - How do I refresh the page when a user is routed in Ionic 5 using Angular 如何使用Ionic 3 / Angular 5正确加载参数并将其绑定到img url? - How to properly load and bind a parameter to a img url using Ionic 3 / Angular 5? 如何在 Ionic 4 / Angular 中禁用页面过渡动画? - How to disable page transition animation in Ionic 4 / Angular? 从第二页向前导航到第三页时页面不会改变,第三页是与第三页具有相同 url 的第一页 Angular Ionic - Page does not change when navigating forward from second page to third page, which is the first page with the same url as the third page Angular Ionic Angular:打开起始页时如何读取 URL 字符串中的参数? - Angular: How to read a parameter in the URL string when opening the start page? 重新进入已在Angular Ionic中初始化的页面时,如何触发事件? - How to trigger an event when re-entering a page that has already been initialized in Angular Ionic? 路由组件的Angular 2“动画幻灯片” - Angular 2 “slide in animation” of a routed component Angular 8 - 如何在桌面和移动设备上使用不同的值触发 animation? - Angular 8 - how to trigger animation with different values on Desktop and Mobile? 当元素在手机上可见时,Ionic 2 触发动画 - Ionic 2 trigger animation when element is in visible on phone 第一次在angular2中呈现时,更改检测在路由页面上不起作用 - change detection not working on routed page when first rendered in angular2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM