Answer:
A resolver is a service that fetches data before the route is activated, ensuring that necessary data is available when the component loads.
@Injectable({ providedIn: 'root' })
export class DataResolver implements Resolve<DataType> {
constructor(private dataService: DataService) {}
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<DataType> {
return this.dataService.getData();
}
}
// In the routing module
const routes: Routes = [
{
path: 'example',
component: ExampleComponent,
resolve: { data: DataResolver }
}
];
No comments:
Post a Comment