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

相关内容

热门资讯

透视真的!wepoker轻量版... 透视真的!wepoker轻量版透视系统,we-poker有人玩吗(透视)真是真的有挂;1、构建自己的...
透视新版!德普之星透视辅助插件... 透视新版!德普之星透视辅助插件(透视)辅助器app(原来真的有挂)该软件可以轻松地帮助玩家将德普之星...
透视有挂!wepoker有透视... 透视有挂!wepoker有透视吗,wejoker私人辅助软件(透视)原来存在有挂;1、该软件可以轻松...
透视神器!德普之星的辅助工具介... 透视神器!德普之星的辅助工具介绍(透视)透视挂(确实真的有挂)1、德普之星的辅助工具介绍ai辅助优化...
透视科技!wepoker插件下... 透视科技!wepoker插件下载,wepoker私人局俱乐部辅助(透视)本来存在有挂1、进入到wep...
透视数据!德普软件(透视)ap... 透视数据!德普软件(透视)app安卓版破解版(切实真的是有挂)1、首先打开德普软件最新版本,在德普软...
透视安卓版!wepoker私人... 透视安卓版!wepoker私人局外卦,we poker辅助器下载(透视)原来存在有挂;1、许多玩家不...
透视私人局!如何下载德普之星辅... 透视私人局!如何下载德普之星辅助软件(透视)透视免费(一贯真的是有挂)小薇(透视辅助)致您一封信;亲...
透视挂!wepoker透视有用... 透视挂!wepoker透视有用吗,wepoker透视功能下载(透视)都是真的是有挂1、实时wepok...
透视挂透视!德普之星怎么设置埋... 透视挂透视!德普之星怎么设置埋牌(透视)辅助器(确实是有挂)所有人都在同一条线上,像星星一样排成一排...