AngularRouteReuseStrategy返回时的问题/跨模块
创始人
2024-10-29 06:00:49
0

在Angular中,当我们使用RouteReuseStrategy来在页面上重复使用一个组件时,有时候会遇到一些返回时的问题,尤其是在进行跨模块的页面跳转时会更加明显。在这种情况下,我们可以通过以下方法来解决这个问题:

1.在应用的module.ts文件中,定义RouteReuseStrategy。

import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { RouteReuseStrategy } from '@angular/router';

import { AppComponent } from './app.component'; import { AppRoutingCache } from './app-routing.cache';

@NgModule({ declarations: [AppComponent], imports: [BrowserModule], providers: [ { // 注入自定义 RouteReuseStrategy provide: RouteReuseStrategy, useClass: AppRoutingCache, }, ], bootstrap: [AppComponent], }) export class AppModule {}

2.定义一个自定义的RouteReuseStrategy类,并继承默认的RouteReuseStrategy。

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

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

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

store( route: ActivatedRouteSnapshot, handle: DetachedRouteHandle ): void { this.handlers[route.url.join('/')] = handle; }

shouldAttach(route: ActivatedRouteSnapshot): boolean { return !!this.handlers[route.url.join('/')]; }

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

shouldReuseRoute( future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot ): boolean { return future.routeConfig === curr.routeConfig; } }

在自定义的RouteReuseStrategy中,我们实现了shouldDetach(), store(), shouldAttach(), retrieve()和shouldReuseRoute()方法。

我们通过shouldDetach()方法允许我们缓存所有页面,store

相关内容

热门资讯

透视科技(德州ai人工智能)鱼... 透视科技(德州ai人工智能)鱼扑克辅助(详细辅助2025新版技巧)其实存在有挂1、很好的工具软件,可...
WePoKe透明挂!云扑克德州... WePoKe透明挂!云扑克德州辅助器,(AAPOKER)一直存在有挂(详细辅助黑科技教程)是一款可以...
透视ai代打(wepoke有挂... 1、透视ai代打(wepoke有挂)aa poker智能ai辅助介绍(详细辅助第三方教程)最初有挂;...
智星德州菠萝外挂!德州wpk辅... 1、智星德州菠萝外挂!德州wpk辅助真的,(来玩德州app)确实有挂(详细辅助必备教程)。2、智星德...
透视新版(德州ai人工智能)哈... 透视新版(德州ai人工智能)哈糖大菠萝怎么拿好牌(详细辅助扑克教程)竟然真的有挂1、在ai机器人技巧...
微扑克辅助挂!德州游戏辅助器大... 微扑克辅助挂!德州游戏辅助器大全,(wepOKE)确实是有挂(详细透视教你攻略);亲真的是有正版授权...
透视实锤(微扑克ai辅助)德扑... 透视实锤(微扑克ai辅助)德扑之星能看到底牌吗(详细辅助黑科技教程)从来真的是有挂,亲,有的,ai轻...
德扑ai助手!菠萝德州app挂... 德扑ai助手!菠萝德州app挂哪里买,(aapoKer)从来是有挂(详细透视辅助教程);值得一提的是...
透视规律(WePoKe透视挂)... 透视规律(WePoKe透视挂)wepoke软件透明挂辅助(详细辅助教你教程)从前是有挂1、每个玩家都...
德州ai机器人!德州ai智能辅... 德州ai机器人!德州ai智能辅助,(Wepoke)确实真的是有挂(详细辅助德州论坛)是一款可以让一直...