Skip to content

Instantly share code, notes, and snippets.

@vmasek
Last active May 2, 2020 12:16
Show Gist options
  • Save vmasek/9974086b84e6a7f77c724348bd7e5fec to your computer and use it in GitHub Desktop.
Save vmasek/9974086b84e6a7f77c724348bd7e5fec to your computer and use it in GitHub Desktop.
Using activated route query parameters to fetch articles
@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