Search code examples
angulartypescriptangular-routing

In Angular2, how to use resolve and promise to wait data load from server before app route render page


I am working on Angular 6 application. I have requirements to load dynamic routes from database. so to achieve this I have injecting Routes service class (DynamicRoutingService) which is responsible to load static and dynamic routes from database and add in app.module.ts --> RouterModule.forRoot(RouteCollection).

In DynamicRoutingService class, I am loading data using subscribe method, my issue is the page of requested url is render before subscribe method get http result hence, application throw error of invalid route.

I am come across to use resolve and promise to solve this but not sure exactly I do that and that is where I need help

app route resolver

Injectable()
export class AppRoutingResolver implements Resolve<any>{

constructor(
    private router: Router
){}

resolve(route: ActivatedRouteSnapshot):Promise<any> | boolean{
    return false;
 }
}

app.component

export class AppComponent {

constructor(private routingService:DynamicRoutingService){
   routingService.initializeDynamicRoutes(); // call service to reload routes
}
}

app.routing.module

@NgModule({
imports: [
  RouterModule.forRoot(RouteCollection)
 ],
 exports: [
  RouterModule
  ]
 })
  export class AppRoutingModule { 

 constructor(
 private router:Router
 ) { 
 }
}

DynamicRoutingService

 @Injectable({
 providedIn: 'root'
 })

 export class DynamicRoutingService {

private dynamicRoutes: Route[] = [];
private waitToLoadRoutes: boolean = true;
private dynamicRoutesData: any;
private routeObject: any;
routeSubject: string = "survey";
static forEach: any;


constructor(
    private router: Router,
    private dynamicRoutesDataServices: DynamicRoutesDataServices
 ) { }

 public initializeDynamicRoutes() {
    this.loadDynamicRoutes();
 }


public loadDynamicRoutes(): Route[] {
    //LOADING DATA FROM DATABASE

this.dynamicRoutesDataServices.GetDynamicRoutesCollection(this.routeSubject)
        .subscribe((result: any) => {
            if (result) {
                this.dynamicRoutesData = result
                if (this.dynamicRoutesData) {
                    this.assembleDynamicRoutes();
                }
            }
        });

    return this.dynamicRoutes;
}

DataService to call http query

export class DynamicRoutesDataServices{

constructor(
    private getDynamicRoutesQuery:GetDynamicRoutesQuery
){}

public GetDynamicRoutesCollection(routesSubject: string): any{
    this.getDynamicRoutesQuery.initialise(routesSubject);
    var result = this.getDynamicRoutesQuery.execute();
    return result;
 }
}

Http Query to call API

@Injectable()
export class GetDynamicRoutesQuery extends BaseQuery<any> 
{

private data:any;

public initialise(dynamicRouteSubject:string): void{
    this.method = RequestMethod.Post;

    if(dynamicRouteSubject){
         this.data = {
            RouteSubject: dynamicRouteSubject
        }
    }

    this.setBody(this.data);

    super.setEndPoint(`myside/GetDynamicRoutes`);
  } 
}

Solution

  • I am not sure if you are using the correct aproach here to load dynamic routes. Resolvers in angular are helpers that prefetch data for an assigned route.

    Example 1:

     RouterModule.forRoot([
          {
            path: 'team/:id',
            component: TeamComponent,
            resolve: {
              team: TeamResolver
            }
          }
        ])
    

    The resolver stores the info of the team in team property in the ActivatedRoute data property. So you need to attach it to an existing route.

    https://angular.io/api/router/Resolve

    https://codeburst.io/understanding-resolvers-in-angular-736e9db71267