I have a component called profile. It gets its data from user.service. Problem is that it does not change when the Route changes (on click, using 'routerLink'); I want to bring in new data from the service every time the route changes. I have tried Subscribing to the route param observable. But it returns an Observable of an Observable (I'm sure there are better ways of doing things).
Here's My Code
// imports ...
@Component({
...
})
export class ProfileComponent implements OnInit {
$user: Observable<User>;
constructor(private userService: UserService, private router: Router, private route: ActivatedRoute) {}
ngOnInit() {
// UserService.getSingleUser(string); returns Observable<User>;
// Method 1 (Tried. failed)
this.$user = this.userService.getSingleUser(this.router.url.sibstring(1));
// Method 2 (Tried, failed)
this.route.params.pipe(
map(params => params.username),
map(username => this.userService.getSingleUser(username))
).subscribe(response => {
this.$user = response;
});
this.$user.subscribe({
next: console.log,
error: console.warn
});
}
}
{path:":username", component: ProfileComponent}- troglodytto