Search code examples
angular6angular-routing

In Angular2, Use Service to get application routes


I have angular 6 application. I have requirements to get dynamic routes, logic of which is been sorted. I have AppRoutingService class which has method getAppRoutes() returning static route collection. I need this to be called app RouterModule.forRoot(...), the objective if can manage to inject static routes using service class, then I can assemble dynamic routes from database in this static list.

In doing so I am getting error.

error

Uncaught TypeError: Cannot read property 'appRoutingService' of undefined
at main.js:1055
at Module../src/app/modules/application/app-routing.module.ts (main.js:1065)
at __webpack_require__ (runtime.js:84)

App routing class

import { AppRoutingService } from './services/routing/app-routing-service';

@NgModule({
imports: [
   RouterModule.forRoot(this.appRoutingService.getAppRoutes()) // here i need to inject appRoutingService method... need help here....???
 ],
   exports: [
   RouterModule
  ]
})
export class AppRoutingModule { 

constructor(
 private router:Router,
 private appRoutingService: AppRoutingService
 ) { 
 }
}

App Routing Service class

@Injectable()
export class AppRoutingService{


public RouteCollection: Routes = [
    {
        path:'',
        redirectTo: 'dashboard',
        pathMatch: 'full'
    },
    {
        path: '',
        component: WebLayoutComponent,
        children:[
            { 
                path:'dashboard', 
                loadChildren: '../dashboard/dashboard.module#DashboardModule'
            },
            { 
                path:'survey', 
                loadChildren: '../survey/survey.module#SurveyModule' 
            }
        ]
    }, 
    { 
        path:'**', 
        component:PageNotFoundComponent
    }
 ];

 public getAppRoutes(): Route[]
 {
    return this.RouteCollection;
 } 
}

Solution

  • You are using the keyword "this" in a wrong place. there is no "this" in that context, because it is outside of a class. Since "this" === undefined, then the reason behind the error becomes clear, because you are trying to fetch the appRoutingService from undefined.

    @NgModule({
    imports: [
       RouterModule.forRoot(AppRoutingService.getAppRoutes()) // here i need 
               to inject appRoutingService method... need help here....???
      ],
      exports: [
        RouterModule
      ]
    })
    

    And make the AppRoutingService method and variable static.

    public static RouteCollection: Routes = [
        {
            path:'',
            redirectTo: 'dashboard',
            pathMatch: 'full'
        },
        {
            path: '',
            component: WebLayoutComponent,
            children:[
            { 
                path:'dashboard', 
                loadChildren: '../dashboard/dashboard.module#DashboardModule'
            },
            { 
                path:'survey', 
                loadChildren: '../survey/survey.module#SurveyModule' 
            }
        ]
       }, 
       { 
           path:'**', 
           component:PageNotFoundComponent
       }
    ];
    
    public static getAppRoutes(): Route[]
    {
       return AppRoutingService.RouteCollection;
    }