I am currently working on an Angular application, and this is my first experience with JS. I have a main view where I display several elements, such as movies, each of which is clickable and links to a detailed view of the movie. My question is how can I handle the ID that I am passing to the detail view in my component's TypeScript file? I need this ID to fetch the corresponding movie object from the API.
Here is the code I have for the detail view:
export class MovieDetailComponent implements OnInit {
public movie: MovieResponse[] = []
constructor(private http: HttpClient, private sanitizer: DomSanitizer) { }
async ngOnInit(): Promise<void> {
const movie = await firstValueFrom(this.http.get<[MovieResponse]>(environment.backend + '/api/v1/movie/' + this.route.snapshot.paramMap.get('id')));
this.movie = await Promise.all(movie.map(async (movie) => {
movie.cover_url = <string>this.sanitizer.bypassSecurityTrustResourceUrl(URL.createObjectURL(await firstValueFrom(this.http.get(movie.cover_url, {responseType: 'blob'}))));
return movie;
}));
}
}
I am having trouble resolving
this.route.snapshot.paramMap.get('id')
. What do I need to do to work with the ID I am passing?
Here is the basic flow I am following:
-> links to the detail view<div [routerLink]="['movie', movie.id]">
- In my app-routing.module.ts file, I have integrated the component for the detail view like this:
const routes: Routes = [ { path: '', canActivate: [AuthGuard], children: [ { path: '', component: MediaComponent }, ... additional routes would be here. { path: 'movie/:id', component: MovieDetailComponent } ] } ];