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

相关内容

热门资讯

手段透视!大菠萝789辅助器下... 手段透视!大菠萝789辅助器下载,aapoker透视方法,wpk教程(有挂攻略)-哔哩哔哩您好:大菠...
发现透视"wpk软件... 发现透视"wpk软件辅助挂"开挂(透视)辅助软件(玩家必用安装教程)发现透视"wpk软件辅助挂"开挂...
诀窍透视!wpk辅助最怕三个东... >>您好:约局吧开挂神器是真的吗确实是有挂的,很多玩家在这款约局吧开挂神器是真的吗游戏中打牌都会发现...
关于透视"wepok... 关于透视"wepoker能开挂吗"开挂(透视)辅助下载(解密关于揭秘攻略)关于透视"wepoker能...
窍门透视!德扑之心免费透视,w... 窍门透视!德扑之心免费透视,wpk辅助插件,解密教程(有挂透明挂)-哔哩哔哩德扑之心免费透视ai黑科...
分享透视"微乐扑克怎... 分享透视"微乐扑克怎么才能赢得大牌"开挂(透视)辅助器(一分钟了解攻略方法)分享透视"微乐扑克怎么才...
绝活儿透视!pokemmo手机... 较多好评“微乐万能挂官网”开挂(透视)辅助教程 了解更多开挂安装加(136704302)微信号是一款...
原来有透视"wpk透... 原来有透视"wpk透视挂"开挂(透视)辅助挂(玩家必看透明教程)原来有透视"wpk透视挂"开挂(透视...
法子透视!德州局脚本,hhpo... 法子透视!德州局脚本,hhpoker德州辅助,新2026教程(有挂头条)-哔哩哔哩hhpoker德州...
科技透视"微扑克辅助... 科技透视"微扑克辅助器官网入口"开挂(透视)辅助安装(必看攻略透视教程)【福星临门,好运相随】;科技...