So I have a resolve in my route as follows. It simply resolves user json.
const routes: Routes = [
{ path: 'profile/:id', component: ProfileEditComponent, pathMatch: 'full',
canActivate: [AuthGuard],
resolve: {
user: ProfileResolveService
}
},
];
My Resolve service is:
@Injectable()
export class ProfileResolveService implements Resolve<any> {
constructor(private service: UserService, private router: Router,
private notificationService: NotificationService) {}
resolve(route: ActivatedRouteSnapshot) {
const id = route.paramMap.get('id');
return this.service.getUserProfile(id).map((response) => {
return response;
}).subscribe(user => user, (error) => {
this.router.navigate(['/login']);
this.notificationService.error('Oops! Something went wrong.', 3000);
return null;
});
}
}
I'm trying to get the resolve in the component and doing the following:
this.activatedRoute.data.map(data => {
return data.user;
}).subscribe((res) => {
console.log('res', res);
});
However, I don't get the value rather I get {user: Subscriber} in the log
can you try below like that
resolve(route: ActivatedRouteSnapshot) {
const id = route.paramMap.get('id') || '';
return this.service.getUserProfile(id)
.map((response: any) => {
return response.data;
}).catch(Error => {
this.notificationService.error('Oops! Something went wrong.', 3000);
this.router.navigate(['/login']);
return null;
});
}
in your component
var user = this.aroute.snapshot.data['user'];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With