I'm struggling with setting default values for my route parameters within the ts class of my component. I would like genreId to default to null, while monetization, sortBy, and page should have preset values. I'm unsure whether I should handle this in ngOnInit or if it should be done differently within my app routing module.
export class MoviesPageComponent implements OnInit {
movies?: MovieData;
genreId?: string | null = null;
monetization?: string | null;
sortBy?: string | null;
page?: string | null;
constructor(private getMovies: GetMoviesService,
private route: ActivatedRoute) { }
ngOnInit(): void {
this.route.queryParamMap.subscribe((params: ParamMap) => {
this.genreId = params.get('genreId')
})
this.route.paramMap.subscribe((params: ParamMap) => {
this.monetization = params.get('monetization');
this.sortBy = params.get('sortBy');
this.page = params.get('page');
});
if (this.genreId === null) {
this.getMovies.moviesData(this.monetization, this.sortBy, this.page).subscribe((movieData: MovieData) => {
if (movieData !== undefined) {
this.movies = movieData;
}
})
}
}
}
This is how my app routing module array is structured.
const routes: Routes = [
{path: ' ', pathMatch: 'full', redirectTo: '/:monetization/:sortBy/:page'},
{path: '/:monetization/:sortBy/:page/', pathMatch: 'full', component: MoviesPageComponent}
];