0
0
Angularframework~20 mins

Resolver for pre-fetching data in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Angular Resolver Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a resolver fails to fetch data?

Consider an Angular resolver that fetches user data before activating a route. If the resolver returns an error or fails, what will happen to the route activation?

Angular
import { Injectable } from '@angular/core';
import { Resolve, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { UserService } from './user.service';

@Injectable({ providedIn: 'root' })
export class UserResolver implements Resolve<any> {
  constructor(private userService: UserService) {}

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<any> {
    return this.userService.getUser(route.params['id']).pipe(
      catchError(error => throwError(() => new Error('Failed to load user')))
    );
  }
}
AThe route activation will be canceled and navigation will not proceed.
BThe route will activate normally but the component will receive undefined data.
CThe route will activate but Angular will display a default error message automatically.
DThe resolver will retry fetching data indefinitely until it succeeds.
Attempts:
2 left
💡 Hint

Think about how Angular handles errors in resolvers and what happens to navigation when a resolver fails.

📝 Syntax
intermediate
2:00remaining
Identify the correct resolver syntax for pre-fetching data

Which of the following Angular resolver implementations correctly pre-fetches data and returns an observable?

A
import { Injectable } from '@angular/core';
import { Resolve, ActivatedRouteSnapshot } from '@angular/router';
import { Observable } from 'rxjs';
import { HttpClient } from '@angular/common/http';

@Injectable({ providedIn: 'root' })
export class DataResolver implements Resolve&lt;any&gt; {
  constructor(private http: HttpClient) {}
  resolve(route: ActivatedRouteSnapshot): Observable&lt;any&gt; {
    return this.http.get('/api/data');
  }
}
B
export class DataResolver implements Resolve {
  resolve(route: ActivatedRouteSnapshot): any {
    return this.http.get('/api/data').subscribe();
  }
}
C
export class DataResolver {
  resolve(route: ActivatedRouteSnapshot): Promise&lt;any&gt; {
    return fetch('/api/data');
  }
}
D
export class DataResolver implements Resolve {
  resolve() {
    return this.http.get('/api/data');
  }
}
Attempts:
2 left
💡 Hint

Remember that resolvers must implement the Resolve interface and return an observable or promise without subscribing inside.

state_output
advanced
2:00remaining
What data does the component receive from the resolver?

Given this route configuration and resolver, what will the component receive as data in ActivatedRoute?

Angular
const routes = [
  {
    path: 'profile/:id',
    component: ProfileComponent,
    resolve: { userData: UserResolver }
  }
];

@Injectable({ providedIn: 'root' })
export class UserResolver implements Resolve<User> {
  constructor(private userService: UserService) {}
  resolve(route: ActivatedRouteSnapshot): Observable<User> {
    return this.userService.getUser(route.params['id']);
  }
}

// In ProfileComponent constructor:
constructor(private route: ActivatedRoute) {
  this.route.data.subscribe(data => {
    console.log(data.userData);
  });
}
AUndefined, because resolvers do not pass data to components.
BAn observable of the user data, not yet resolved.
CAn object containing the user data fetched by the resolver.
DThe route parameters object with the user ID.
Attempts:
2 left
💡 Hint

Resolvers provide data to the route's data observable after resolving.

🔧 Debug
advanced
2:00remaining
Why does this resolver cause a runtime error?

Examine the following resolver code. Why does it cause a runtime error when navigating to the route?

Angular
export class ProductResolver implements Resolve<Product> {
  constructor(private productService: ProductService) {}

  resolve(route: ActivatedRouteSnapshot): Product {
    this.productService.getProduct(route.params['id']).subscribe(product => {
      return product;
    });
  }
}
ABecause the resolver subscribes inside and returns the subscription instead of data.
BBecause the resolver returns void instead of an observable or promise.
CBecause the ProductService method getProduct does not exist.
DBecause the resolver does not implement the Resolve interface correctly.
Attempts:
2 left
💡 Hint

Resolvers must return an observable or promise, not void or subscription.

🧠 Conceptual
expert
2:00remaining
How to handle multiple resolvers for a single route?

You want to pre-fetch both user data and settings before activating a route. Which approach correctly uses multiple resolvers in Angular routing?

AUse multiple <router-outlet> elements each with its own resolver.
BUse an array of resolvers in the route: <code>resolve: [UserResolver, SettingsResolver]</code>
CChain resolvers inside one resolver class that calls both services sequentially.
DUse an object with keys for each resolver: <code>resolve: { user: UserResolver, settings: SettingsResolver }</code>
Attempts:
2 left
💡 Hint

Think about how Angular expects the resolve property to be structured for multiple resolvers.