Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 Routing: import submodule with routing + making it prefixed

I have a main module and some submodules. And I want to specify some not trivial routing between them.

I'd prefer defining the routes of a submodule within the submodule. E.g.:

@NgModule({
    imports: [
        /*...*/
        RouterModule.forChild([
            { path: 'country', redirectTo: 'country/list' },
            { path: 'country/list', component: CountryListComponent },
            { path: 'country/create', component: CountryCreateComponent },
            /*...*/
        ])
    ],
    declarations: [/*...*/],
    exports: [
        RouterModule,
    ],
})
export class CountryModule {}

I want to import this module with its own internal routing, but I want to make its whole routing prefixed.

const appRoutes = [
    { path: '', component: HomeComponent },
    /*... (basic routes)*/
];

@NgModule({
    imports: [
        /*...*/
        RouterModule.forRoot(appRoutes),
        CountryModule, // <- how to make its routing prefixed??
    ],
    declarations: [
        /*...*/
        AppComponent,
    ],
    bootstrap: [ AppComponent ]
})
export class AppModule {}

This settings creates the following routes: /country, /country/list, etc., but I want to make them prefixed like this:

  • /settings/country
  • /settings/country/list
  • /settings/country/create

There are other modules that I want to access via another routing, e.g. a CityModule under /otherstuff/city/create and /otherstuff/city/list`.

My questions:

  1. Is it possible to import a module with its own routing and make its routes prefixed?
  2. Furthermore: Is there a way to make links between 2 submodules agnostically of their final (prefixed) routes?

UPDATE

The accepted answer is the best way to do it: create the routes in the module, register them externally. Thus you can modify the routes, e.g. prefix them (this is what I wanted), you can define guards, override or filter them, etc.

like image 545
Gábor Imre Avatar asked Dec 07 '16 14:12

Gábor Imre


People also ask

How to create a routing module in angular?

Create an Angular App using Angular CLI command. Lets create a feature module named AdminModule . First, Create a folder called admin under app folder. The next step is to create the routing module for the above components.

What are modules in angular?

The Modules are core of any Angular apps. Our App will going to contain several such modules each implementing a specific feature. If you are new to routing, then suggest you to read the following articles

What is ngroute in AngularJS?

The ngRoute module routes your application to different pages without reloading the entire application. What do I Need? To make your applications ready for routing, you must include the AngularJS Route module: Then you must add the ngRoute as a dependency in the application module:

How to make your angular application modular?

To make our Angular application modular, it is best to assign the routes module-wise. Therefore, we will take how we can create root routes and child routes in this angular routing and sub routing example. To navigate to different pages in your application, you also want the application to be a SPA (Single Page Application), with no page reloading.


2 Answers

Playing with this Routing stuff I just found a clean way I would like to share, to handle the routes of submodules with no headaches and love Angular even more. Taking the OP case as an example, I propose you to study the following code:

Add a utility function to your CountryModule submodule, to load it dynamically from the Router and avoid a compiler warning about replacing the arrow function with a reference to an exported function:

@NgModule({
  imports: [
    ...
    RouterModule.forChild([
      { path: 'country', pathMatch: 'full', redirectTo: 'list' },
      { path: 'country/list', component: CountryListComponent },
      { path: 'country/create', component: CountryCreateComponent },
    ])
  ],
  declarations: [ ... ],
  exports: [
    RouterModule,
  ],
})
export class CountryModule {}

export function CountryEntrypoint() {
  return CountryModule;
}

Now you can import that Entrypoint into your parent module where you want to place the routes:

@NgModule({
  imports: [
    ...
    RouterModule.forRoot([
      { path: '', pathMatch: 'full', component: HomeComponent },
      { path: 'settings', loadChildren: CountryEntrypoint }
    ]),
  ],
  declarations: [AppComponent],
  bootstrap: [AppComponent]
})
export class AppModule {}

And there you go! You can now reach your submodule components with settings/country/list and settings/country/create.

WARNING

Be careful to not import the CountryModule into your parent module's @NgModule, because it will override the routes outside the settings path. Let the router do the job.

Enjoy!

like image 103
Mateo Tibaquira Avatar answered Sep 24 '22 10:09

Mateo Tibaquira


in your appRoutes add child route like

const appRoutes = [
    { path: '', component: HomeComponent },
    {
    path: 'settings',
    component: CountryComponent,
    canActivate: [AuthGuard],
    children: COUNTRY_ROUTES
  },
];

Create a separate routes file

export const COUNTRY_ROUTES:Routes = [
  { path: 'country', redirectTo: 'country/list' },
  { path: 'country/list', component: CountryListComponent },
  { path: 'country/create', component: CountryCreateComponent },

];

In CountryComponent.html

<router-outlet></router-outlet>
like image 31
Md Ayub Ali Sarker Avatar answered Sep 22 '22 10:09

Md Ayub Ali Sarker