Atanu Samanta
Atanu Samanta

Reputation: 175

Custom middleware in angular

I am using angular 8. Can I make a custom middleware for checking network speed and local storage checking before any component load. Can you please suggest me how to do it?

Upvotes: 0

Views: 2120

Answers (1)

piedpiper
piedpiper

Reputation: 524

Try using resolvers which is called before any component load like this-

custom.resolver.ts

import {
  Resolve,
  ActivatedRouteSnapshot,
  RouterStateSnapshot,
  Router
} from '@angular/router';
import { Inject } from '@angular/core';

export class ActiveResolver implements Resolve<any> {
  constructor() {}

  resolve(_route: ActivatedRouteSnapshot, _rstate: RouterStateSnapshot) {
       <-- your custom logic for speed -->
  }

}

In your app.module.ts

RouterModule.forRoot([     
      {
        path: 'yourcomponent',
        component: yourcomponent,
        pathMatch: 'full',
        resolve: { ar: ActiveResolver }
      }]

Upvotes: 1

Related Questions