admin管理员组文章数量:1193986
Below route reuse is not working. Route changes and reloads CustomRouteReuseStrategy
. Will it work or any alternate way?
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, DetachedRouteHandle, RouteReuseStrategy } from '@angular/router';
import { NavigationStart, Router } from '@angular/router';
@Injectable({
providedIn: 'root',
})
export class CustomRouteReuseStrategy implements RouteReuseStrategy {
private storedRoutes = new Map<string, DetachedRouteHandle>();
shouldDetach(route: ActivatedRouteSnapshot): boolean {
return true; // Allow detaching
}
store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle | null): void {
if (handle) {
this.storedRoutes.set(route.routeConfig?.path ?? '', handle);
}
}
shouldAttach(route: ActivatedRouteSnapshot): boolean {
return this.storedRoutes.has(route.routeConfig?.path ?? '');
}
retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle | null {
return this.storedRoutes.get(route.routeConfig?.path ?? '') || null;
}
shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
return future.routeConfig === curr.routeConfig;
}
}
Below route reuse is not working. Route changes and reloads CustomRouteReuseStrategy
. Will it work or any alternate way?
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, DetachedRouteHandle, RouteReuseStrategy } from '@angular/router';
import { NavigationStart, Router } from '@angular/router';
@Injectable({
providedIn: 'root',
})
export class CustomRouteReuseStrategy implements RouteReuseStrategy {
private storedRoutes = new Map<string, DetachedRouteHandle>();
shouldDetach(route: ActivatedRouteSnapshot): boolean {
return true; // Allow detaching
}
store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle | null): void {
if (handle) {
this.storedRoutes.set(route.routeConfig?.path ?? '', handle);
}
}
shouldAttach(route: ActivatedRouteSnapshot): boolean {
return this.storedRoutes.has(route.routeConfig?.path ?? '');
}
retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle | null {
return this.storedRoutes.get(route.routeConfig?.path ?? '') || null;
}
shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
return future.routeConfig === curr.routeConfig;
}
}
Share
Improve this question
edited Jan 29 at 15:36
ManiTeja
asked Jan 23 at 14:20
ManiTejaManiTeja
8471 gold badge9 silver badges13 bronze badges
1 Answer
Reset to default 0import { Injectable } from '@angular/core';
import { Router, NavigationStart } from '@angular/router';
import { BehaviorSubject } from 'rxjs';
import { filter } from 'rxjs/operators';
@Injectable({
providedIn: 'root',
})
export class NavigationTrackerService {
private originalUrls = new Map<string, string>(); // To store original URLs
public url$ = new BehaviorSubject<string>('');
constructor(private router: Router) {
// Subscribe to NavigationStart to track original URLs
this.router.events
.pipe(filter((event) => event instanceof NavigationStart))
.subscribe((event: NavigationStart) => {
const url = event.url;
this.url$.next(url); // Broadcast the URL
const key = this.getKeyFromUrl(url);
if (key && !this.originalUrls.has(key)) {
this.originalUrls.set(key, url); // Cache the original URL
}
});
}
getOriginalUrl(key: string): string | undefined {
return this.originalUrls.get(key);
}
private getKeyFromUrl(url: string): string {
return url; // You can customize this logic as needed
}
}
import {
RouteReuseStrategy,
ActivatedRouteSnapshot,
DetachedRouteHandle,
} from '@angular/router';
import { Injectable } from '@angular/core';
import { NavigationTrackerService } from './navigation-tracker.service';
@Injectable({
providedIn: 'root',
})
export class CustomRouteReuseStrategy implements RouteReuseStrategy {
private storedRoutes: { [key: string]: DetachedRouteHandle } = {};
constructor(private navigationTracker: NavigationTrackerService) {}
shouldDetach(route: ActivatedRouteSnapshot): boolean {
return true;
}
store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle | null): void {
const key = this.getRouteKey(route);
if (key) {
this.storedRoutes[key] = handle;
}
}
shouldAttach(route: ActivatedRouteSnapshot): boolean {
const key = this.getRouteKey(route);
return !!this.storedRoutes[key];
}
retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle | null {
const key = this.getRouteKey(route);
return key ? this.storedRoutes[key] : null;
}
shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
const futureKey = this.getRouteKey(future);
const currKey = this.getRouteKey(curr);
// Use NavigationTrackerService to compare original URLs if needed
const futureUrl = this.navigationTracker.getOriginalUrl(futureKey);
const currUrl = this.navigationTracker.getOriginalUrl(currKey);
return futureUrl === currUrl && future.routeConfig === curr.routeConfig;
}
private getRouteKey(route: ActivatedRouteSnapshot): string | null {
const path = route.routeConfig?.path;
const params = JSON.stringify(route.params);
return path ? `${path}_${params}` : null;
}
}
本文标签: typescriptAngular custom RouteReuseStrategy not workingStack Overflow
版权声明:本文标题:typescript - Angular custom RouteReuseStrategy not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738488807a2089583.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论