I am trying to create routes for user profiles in Angular. The snapshot, no-observable approach, almost behaves like I want; where the getUser() function outputs the user
object in my template. It falls short in that I can't update the user
once I go to it. I think I need the switchMap approach so that I can regenerate the instance for different users in the same session.
What do I need to do to make the user
variable available in my template with switchMap? In both cases the router works (goes to the generic template), but with switchMap, the user
object doesn't return anything in the template.
constructor(private apiService: ApiService,
private form_builder: FormBuilder,
public authService: AuthService,
private route: ActivatedRoute,
private router: Router)
ngOnInit(): void {
// const auth0_user_id_synced = this.route.snapshot.paramMap.get('auth0_user_id_synced')!;
// this.getUser(this.route.snapshot.paramMap.get('auth0_user_id_synced'));
this.user$ = this.route.paramMap.pipe(
switchMap((params: ParamMap) =>
this.apiService.getUser(params.get('auth0_user_id_synced')!))
);
// this.getUser('auth0_user_id_synced');
}
getUser(user_pk: any) {
this.apiService.getUser(user_pk)
.subscribe((user:any) => {this.user = user;
},
(error:any) => this.errorMessage = <any>error);
}
I tried both of the below in the template.
<h2>Welcome {{user.first_name}}</h2>
and
<h2>Welcome {{user$.first_name}}</h2>