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

相关内容

热门资讯

wpk透视辅助测试!wpk辅助... wpk透视辅助测试!wpk辅助,wPk的确是有挂的,详细教程,(有挂解说)1、下载好wpk辅助软件之...
德州微扑克辅助!德州透视辅助,... 德州微扑克辅助!德州透视辅助,德州扑克本来是真的有挂(详细ai机器人教程)1、超多福利:超高返利,海...
软件教程!wepoke外 挂(... WePoke高级策略深度解析‌;软件教程!wepoke外 挂(wepoke透明挂黑科技)wepOkE...
wpk有外 挂!wPK的确真的... wpk有外 挂!wPK的确真的有挂,WpK都是真的有挂,技巧教程(有挂解说)1)wpk辅助挂:进一步...
德扑之星作弊!德扑之星可以设置... 德扑之星作弊!德扑之星可以设置埋牌,德州wpk一贯存在有挂(详细有辅助挂教程)1、让任何用户在无需德...
wepoke模拟器!wepok... wepoke模拟器!wepoke辅助(透明挂)wEPoke(的确是有挂的)这是由厦门游乐互动科技有限...
wpk透明挂!wpk微扑克有挂... wpk透明挂!wpk微扑克有挂,WPK就是真的有挂,详细教程,(有挂辅助);1、金币登录送、破产送、...
德扑之星猫腻!德州之星辅助器哪... 1、德扑之星猫腻!德州之星辅助器哪里买,nzt德州确实是真的有挂(详细ai代打教程);详细教程。2、...
wepoke辅助插件!wepo... wepoke辅助插件!wepoke有挂,WEpoke好像真的有挂,介绍教程(有挂揭秘);建议优先通过...
wpk辅助挂(wpK)wpk外... wpk辅助挂(wpK)wpk外 挂被实锤(辅助挂)一贯真的有挂(黑科技教程)1、首先打开wpk最新版...