Insights on my Application (Angular 12):
- Comprises of 3 Modules, each containing an overview page with a list and specific detail pages
- Each route is assigned an area tag to identify the user's navigation within the module
Goal for Angular´s RouteReuseStrategy implementation:
Reusing the list component when a user navigates from list -> detail page and uses the back button (detect back button trigger)
Avoiding the reuse of the list component when a user navigates to it from a different module / area
Reusing the detail component when a user navigates from one detail page to another (default behavior?)
Clearing / destroying stored components when a user leaves a module by navigating elsewhere or logging out
Current Status:
Implemented a custom RouteReuseStrategy successfully that reuses the list component ✓
- Issue with scroll position restoration which requires separate investigation ✕
Attempting to check the areatag within the route, but ActivatedRouteSnapshots are empty ✕
Facing difficulties in detecting back button press due to frequent event firing and issues with implementing a basic back flag ✕
Missing Components:
Detecting back button navigation and adjusting component reuse accordingly
Identifying the module of the route to modify reuse behavior or clean up stored components
Code Snippets:
Sample route in Module A
{
path: 'lista',
component: ListAComponent,
data: {
title: 'List overview',
areaCategory: AreaCategory.A,
reuseRoute: true,
},
},
{
path: 'lista/:id',
component: DetailAComponent,
data: {
title: 'Detail',
areaCategory: AreaCategory.A,
reuseRoute: false,
},
},
Sample route in Module B
{
path: 'listb',
component: ListBComponent,
data: {
title: 'List overview',
areaCategory: AreaCategory.B,
reuseRoute: true,
},
},
{
path: 'listb/:id',
component: DetailBComponent,
data: {
title: 'Detail',
areaCategory: AreaCategory.B,
reuseRoute: false,
},
},
app.module.ts
providers: [
{
provide: RouteReuseStrategy,
useClass: CustomReuseRouteStrategy,
}
],
Would this placement suffice globally or should it be moved to each of the 3 modules?
ReuseRouteStrategy Implementation:
@Injectable()
export class CustomReuseRouteStrategy implements RouteReuseStrategy {
// Code details omitted for brevity
The inconsistency in the future & current snapshot prompts the need to correctly identify routes/components to access areaCategory for desired functionality.
If you spot any discrepancies or have insights to share, your assistance would be greatly valued.
For further reference, here is a link to a simplified stackblitz showcasing my setup