Skip to content

Commit 0817e4b

Browse files
authored
docs: use takeUntilDestroyed in component category list (#33863)
Replace the deprecated `combineLatest` result selector with a `map` operator and clean up the route params subscription with `takeUntilDestroyed` instead of a manual `Subscription` and `ngOnDestroy`.
1 parent 8834f4c commit 0817e4b

1 file changed

Lines changed: 22 additions & 22 deletions

File tree

‎docs/src/app/pages/component-category-list/component-category-list.ts‎

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {ChangeDetectionStrategy, Component, OnDestroy, OnInit, inject} from '@angular/core';
10-
import {ActivatedRoute, RouterLink} from '@angular/router';
9+
import {ChangeDetectionStrategy, Component, DestroyRef, OnInit, inject} from '@angular/core';
10+
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
11+
import {ActivatedRoute, Params, RouterLink} from '@angular/router';
1112
import {MatRipple} from '@angular/material/core';
1213
import {NgTemplateOutlet} from '@angular/common';
13-
import {combineLatest, Subscription} from 'rxjs';
14+
import {combineLatest} from 'rxjs';
15+
import {map} from 'rxjs/operators';
1416

1517
import {
1618
DocItem,
@@ -28,33 +30,31 @@ import {ComponentPageTitle} from '../page-title/page-title';
2830
imports: [NavigationFocus, RouterLink, MatRipple, NgTemplateOutlet],
2931
changeDetection: ChangeDetectionStrategy.Eager,
3032
})
31-
export class ComponentCategoryList implements OnInit, OnDestroy {
33+
export class ComponentCategoryList implements OnInit {
3234
private readonly _docItems = inject(DocumentationItems);
3335
private readonly _componentPageTitle = inject(ComponentPageTitle);
3436
private readonly _route = inject(ActivatedRoute);
37+
private readonly _destroyRef = inject(DestroyRef);
3538

3639
items: DocItem[] = [];
3740
section = '';
38-
routeParamSubscription: Subscription = new Subscription();
3941
_categoryListSummary: string | undefined;
4042

4143
ngOnInit() {
42-
this.routeParamSubscription = combineLatest(
43-
this._route.pathFromRoot.map(route => route.params),
44-
Object.assign,
45-
).subscribe(async params => {
46-
const sectionName = params['section'];
47-
const section = SECTIONS[sectionName];
48-
this._componentPageTitle.title = section.name;
49-
this._categoryListSummary = section.summary;
50-
this.section = sectionName;
51-
this.items = await this._docItems.getItems(sectionName);
52-
});
53-
}
54-
55-
ngOnDestroy() {
56-
if (this.routeParamSubscription) {
57-
this.routeParamSubscription.unsubscribe();
58-
}
44+
combineLatest(this._route.pathFromRoot.map(route => route.params))
45+
.pipe(
46+
map(allParams =>
47+
allParams.reduce((merged, params) => ({...merged, ...params}), {} as Params),
48+
),
49+
takeUntilDestroyed(this._destroyRef),
50+
)
51+
.subscribe(async params => {
52+
const sectionName = params['section'];
53+
const section = SECTIONS[sectionName];
54+
this._componentPageTitle.title = section.name;
55+
this._categoryListSummary = section.summary;
56+
this.section = sectionName;
57+
this.items = await this._docItems.getItems(sectionName);
58+
});
5959
}
6060
}

0 commit comments

Comments
 (0)