Last active
May 2, 2020 12:16
-
-
Save vmasek/9974086b84e6a7f77c724348bd7e5fec to your computer and use it in GitHub Desktop.
Using activated route query parameters to fetch articles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({ | |
selector: 'app-articles-list', | |
templateUrl: './articles-list.component.html', | |
styleUrls: ['./articles-list.component.scss'], | |
}) | |
export class ArticlesListComponent implements OnDestroy { | |
readonly defaultPageSize = 6; | |
private readonly unsubscribe$ = new Subject<void>(); | |
constructor( | |
articles: ArticlesService, | |
route: ActivatedRoute, | |
) { | |
route.queryParams | |
.pipe(takeUntil(this.unsubscribe$.asObservable())) | |
.subscribe(({ page, category, author }) => { | |
articles.fetchArticles({ | |
limit: this.defaultPageSize, | |
// pages are indexed from 1 | |
skip: (parseInt(page, 10) - 1) * this.defaultPageSize, | |
author, | |
category, | |
}); | |
}); | |
} | |
ngOnDestroy(): void { | |
// event if we are just demostrating functionality | |
// we have to take care of our subscription | |
// (use async pipe whenever possible to get rid of this) | |
this.unsubscribe$.next(); | |
this.unsubscribe$.complete(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment