Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the route data resolve value?

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

like image 963
JyotiChhetri Avatar asked Nov 30 '25 14:11

JyotiChhetri


1 Answers

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'];
like image 115
Robert Avatar answered Dec 02 '25 03:12

Robert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!