Angular嵌套路由输出器的子级在路由动画完成之前消失
创始人
2024-10-29 03:00:30
0

要解决"Angular嵌套路由输出器的子级在路由动画完成之前消失"的问题,可以尝试以下解决方案:

  1. 使用Angular的routeReuseStrategy来重用组件,以避免在路由切换时组件被销毁和重新创建。在你的路由模块中,可以将routeReuseStrategy设置为AppRoutingCacheReuseStrategy(自定义的路由重用策略):
import { NgModule } from '@angular/core';
import { RouterModule, RouteReuseStrategy, DefaultUrlSerializer } from '@angular/router';

import { AppRoutingCacheReuseStrategy } from './app-routing-cache-reuse-strategy';

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  providers: [
    {
      provide: RouteReuseStrategy,
      useClass: AppRoutingCacheReuseStrategy
    }
  ],
  exports: [RouterModule]
})
export class AppRoutingModule { }

自定义的AppRoutingCacheReuseStrategy可以参考以下示例代码:

import { RouteReuseStrategy, ActivatedRouteSnapshot, DetachedRouteHandle } from '@angular/router';

export class AppRoutingCacheReuseStrategy implements RouteReuseStrategy {
  private handlers: { [key: string]: DetachedRouteHandle } = {};

  shouldDetach(route: ActivatedRouteSnapshot): boolean {
    return true;
  }

  store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void {
    this.handlers[route.routeConfig.path] = handle;
  }

  shouldAttach(route: ActivatedRouteSnapshot): boolean {
    return !!route.routeConfig && !!this.handlers[route.routeConfig.path];
  }

  retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle {
    if (!route.routeConfig) return null;
    return this.handlers[route.routeConfig.path];
  }

  shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
    return future.routeConfig === curr.routeConfig;
  }
}
  1. 在你的动画完成之前不要销毁子级组件。可以在父级组件的模板文件中使用*ngIf指令来控制子级组件的显示和隐藏。

在父级组件的控制器中,可以定义一个showChildComponent属性来控制子级组件的显示和隐藏,并在动画完成后设置其值为true

import { Component, OnInit } from '@angular/core';
import { trigger, state, style, animate, transition } from '@angular/animations';

@Component({
  selector: 'app-parent-component',
  template: `
    
`, animations: [ trigger('yourAnimation', [ state('void', style({ opacity: 0 })), state('*', style({ opacity: 1 })), transition('void => *', animate('300ms')), transition('* => void', animate('300ms')) ]) ] }) export class ParentComponent implements OnInit { showChildComponent: boolean = false; ngOnInit() { // 模拟动画完成后显示子级组件 setTimeout(() => { this.showChildComponent = true; }, 300); } getState() { return this.showChildComponent ? '*' : 'void'; } }

这样,在路由切换时,子级组件将不会被销毁,直到动画完成后才会显示出来。

以上是解决"Angular嵌套路由输出器的子级在路由动画完成之前消失"的两种方法,你可以根据具体情况选择适合你的解决方案。

相关内容

热门资讯

九分钟教程!微乐小程序黑科技,... 九分钟教程!微乐小程序黑科技,微乐自建房插件详细教程(开挂)其实有挂(2026)1、微乐小程序黑科技...
2026版教学!微乐小程序黑科... 2026版教学!微乐小程序黑科技,小程序微乐辅助器免费版(透视)好像真的有挂(哔哩哔哩)小程序微乐辅...
六分钟教程!微乐小程序黑科技,... 六分钟教程!微乐小程序黑科技,微乐自建房怎么提高运气(开挂)都是真的是有挂(2026)1、首先打开微...
透视系统!微乐小程序黑科技,陕... 透视系统!微乐小程序黑科技,陕西微乐小程序修改器(透视)原来有挂(哔哩哔哩)1、不需要AI权限,帮助...
第4分钟下载!微乐小程序黑科技... 第4分钟下载!微乐小程序黑科技,微乐自建房辅助软件下载(开挂)好像真的是有挂(2026)1、许多玩家...
在玩家背景下!微乐小程序黑科技... 在玩家背景下!微乐小程序黑科技,微乐小程序辅助器出租(透视)都是是真的挂(哔哩哔哩)暗藏猫腻,小编详...
一分钟开挂!微乐小程序黑科技,... 一分钟开挂!微乐小程序黑科技,微乐科技软件下载(开挂)竟然是有挂(2026)1、微乐小程序黑科技免费...
推出新举措!微乐小程序免费黑科... 推出新举措!微乐小程序免费黑科技,微信小程序怎么开挂(透视)竟然真的是有挂(哔哩哔哩)1、每一步都需...
4分钟透视!微乐小程序免费黑科... 4分钟透视!微乐小程序免费黑科技,微乐小程序怎么挂脚本(开挂)好像是真的挂(2026)1)微乐小程序...
透视中牌率!微乐小程序黑科技,... 透视中牌率!微乐小程序黑科技,微乐自建房辅助工具官网(透视)好像是有挂(哔哩哔哩);1、金币登录送、...