Brian
Brian

Reputation: 615

How to Route to Feature Module

I'm trying to have a route from one module include a component from another module. The scheme I'm trying to implement is: /group/feature which should show the content in feature.html included in the content of group.html where the router-outlet is. I can get /group to work, but /group/feature returns page not found.

The full project is available here: https://stackblitz.com/edit/angular-nsfgyr

Here are the main files:

feature.module.ts:

@NgModule({
  imports: [
    CommonModule
  ],
  declarations: [FeatureComponent],
  exports: [FeatureComponent]
})
export class FeatureModule {
}

group-module.ts

@NgModule({
  imports: [
    CommonModule,
    FeatureModule,
    GroupRoutingModule,
  ],
  declarations: [
    GroupComponent,
  ],
  exports: [GroupComponent]
})
export class GroupModule {
}

group-routing.module.ts:

const ingestRoutes: Routes = [
  {path: 'feature', component: FeatureComponent}
];

@NgModule({
  imports: [
    RouterModule.forChild(ingestRoutes)
  ],
  exports: [
    RouterModule
  ]
})
export class GroupRoutingModule {
}

app-routing.module:

const routes: Routes = [
    {path: '', pathMatch: 'full', redirectTo: 'group'},
    {path: 'group', component: GroupComponent},
    {path: '**', component: PageNotFoundComponent}
  ]
;

@NgModule({
  imports: [
    RouterModule.forRoot(
      routes,
      {enableTracing: false, useHash: false}
    )
  ],
  exports: [
    RouterModule
  ]
})
export class AppRoutingModule {
}

Upvotes: 1

Views: 2506

Answers (3)

Brian
Brian

Reputation: 615

I figured it out: Adding the feature path in the children array of the group router worked:

In group-routing.module.ts:

const ingestRoutes: Routes = [
  {path: 'group', component: GroupComponent,
   children: [
     {path: 'feature', component: FeatureComponent}
  ]},
];

Thank you for all the responses and help!

Upvotes: 0

harold_mean2
harold_mean2

Reputation: 238

The first way. No children, no modules imported.

const routes: Routes = [
  {path: '', pathMatch: 'full', redirectTo: 'group'},
  {path: 'group/feature', component: FeatureComponent },
  {path: 'group', component: GroupComponent},
  {path: '**', component: PageNotFoundComponent}
];

@NgModule({
  imports: [
  RouterModule.forRoot(routes,
  {enableTracing: false, useHash: false}
  )
],
exports: [
  RouterModule
]
})
export class AppRoutingModule {
}

The second way is to use children..

const routes: Routes = [
  {path: '', pathMatch: 'full', redirectTo: 'group'},
  {path: 'group', component: GroupComponent,
   children: [
    { path: 'feature', component: FeatureComponent }]     
  },
  {path: '**', component: PageNotFoundComponent}
];

The third way is to import the module. It has its own

"<router-outlet></router-outlet>".

const ingestRoutes: Routes = [
  { path: 'group/feature', component: FeatureComponent,
    children: [       
      path: 'more', component: MoreComponent, // if you want to add..
        // this will route to group/feature/more
      path: '', component: FeatureHomeComponent,
    ] 
  }
];

@NgModule({
  imports: [
    RouterModule.forChild(ingestRoutes)
  ],
  declarations: [
      FeatureComponent, FeatureHomeComponent, MoreComponent
  ],
  exports: [
    RouterModule
  ]
})
export class GroupRoutingModule {
}

The FeatureComponent.html has only

<route-outlet></router-outlet>. 

FeatureHomeComponent.html

- you can display anything here..YOUR ACTURE FEATURE COMPONENT.HTML

Export this in AppRoutingModule.

 const routes: Routes = [
    {path: '', pathMatch: 'full', redirectTo: 'group'},
    {path: 'group', component: GroupComponent},
    {path: '**', component: PageNotFoundComponent}
  ];

@NgModule({
  imports: [
    RouterModule.forRoot(routes, {enableTracing: false, useHash: 
    false}), GroupRoutingModule, // add this..
  ],
  exports: [
    RouterModule
  ]
})
export class AppRoutingModule {
}

All I do for 'apple brand' is to just import AppleRoutingModule in my AppRoutingModule. See link.Angular 4 route selection for category/subcategory or category/item for ecommerce web site

Upvotes: 3

Russell D
Russell D

Reputation: 346

You have defined the route for 'group'. You haven't defined the route 'group/feature'.

Update:

Your group route looks like a plain route to /group, but group is actually a child module and you don't load the component, you loadChildren.

const routes: Routes = [
    {path: '', pathMatch: 'full', redirectTo: 'group'},
    {path: 'group', loadChildren: 'app/group/group.module#GroupModule},
    {path: '**', component: PageNotFoundComponent}
  ]
;

Upvotes: 2

Related Questions