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嵌套路由输出器的子级在路由动画完成之前消失"的两种方法,你可以根据具体情况选择适合你的解决方案。

相关内容

热门资讯

详细透视!hhpoker免费辅... 详细透视!hhpoker免费辅助器,hhpoker脚本下载,果然真的有辅助app(哔哩哔哩)1、游戏...
推荐透视!aapoker公共底... 推荐透视!aapoker公共底牌,aapoker脚本,竟然是有辅助神器(哔哩哔哩)1、起透看视 辅助...
详细透视!wpk刷入池率脚本,... 详细透视!wpk刷入池率脚本,wpk德州局透视,原来是真的有辅助方法(哔哩哔哩)1、玩家可以在德州局...
揭露透视!aapoker脚本怎... 揭露透视!aapoker脚本怎么用,aapoker怎么拿好牌,都是一直总是有辅助方法(哔哩哔哩)1、...
教你透视!hhpoker买挂,... 教你透视!hhpoker买挂,hhpkoer辅助器,本来真的有辅助app(哔哩哔哩)1、游戏颠覆性的...
辅助透视!hhpoker有没有... 辅助透视!hhpoker有没有作比弊挂,德扑HHpoker有挂吗,切实一直总是有辅助方法(哔哩哔哩)...
揭露透视!aapoker透视怎... 揭露透视!aapoker透视怎么用,aapoker公共底牌,确实一直都是有辅助工具(哔哩哔哩)1)有...
了解透视!wpk德州局透视,w... 了解透视!wpk德州局透视,wpk透视是真的假的,总是一直总是有辅助教程(哔哩哔哩)德州局透视能透视...
解迷透视!hhpoker是真的... 解迷透视!hhpoker是真的还是假的,hhpoker有没有辅助,其实是真的有辅助神器(哔哩哔哩)一...
了解透视!wpk辅助器,wpk... 了解透视!wpk辅助器,wpk俱乐部是真的吗,一直真的是有辅助插件(哔哩哔哩)1、下载好透视辅助下载...