1
votes

To sharpen my skills I m trying all idea's that I get in angular

In this context I m trying to route for a feature module composed of two feature modules and a shared module.

My rooting was generated using the cli commande ng g m router --routing and hand updated as follow

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { InterpolationComponent } from '../exemple100/interpolation/interpolation.component'
import { PageNotFoundComponent } from '../page-not-found/page-not-found.component';
import { AppComponent } from '../app.component';
import { Ex200Component } from '../ex200/ex200.component';
import { Ex300Component } from '../ex300/ex300.component';
import { Ex400Component } from '../ex400/ex400.component';
import { Ex500Component } from '../ex500/ex500.component';
import { SalesComponent } from '../ex500/sales/sales.component';
import { SupportComponent } from '../ex500/support/support.component';

const routes: Routes = [
  {path: 'ex100', component: InterpolationComponent },
  {path: 'ex200', component: Ex200Component },
  {path: 'ex300', component: Ex300Component },
  {path: 'ex400', component: Ex400Component },
  {path: 'ex500', component: Ex500Component },
  {path: 'ex500/sales', component: SalesComponent },
  {path: 'ex500/support', component: SupportComponent },
  {path: '**', component: PageNotFoundComponent },
  {path: '', component: AppComponent }
];

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

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { RouterRoutingModule } from './router-routing.module';

@NgModule({
  declarations: [],
  imports: [
    CommonModule,
    RouterRoutingModule
  ]
})
export class RouterModule { }

then I manually update the bootstraping app module

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { InterpolationComponent } from './exemple100/interpolation/interpolation.component';
import { Ex200Component } from './ex200/ex200.component';
import { Ex300Component } from './ex300/ex300.component';
import { FormsModule } from '@angular/forms';
import { Ex400Component } from './ex400/ex400.component';
import { PageNotFoundComponent } from './page-not-found/page-not-found.component';
import { RouterModule } from './router/router.module';
import { Ex500Module } from './ex500/ex500.module';

@NgModule({
  declarations: [
    AppComponent,
    InterpolationComponent,
    Ex200Component,
    Ex300Component,
    Ex400Component,
    PageNotFoundComponent
  ],
  imports: [BrowserModule, FormsModule, RouterModule, Ex500Module],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {}

The Ex500Module is my composed feature module of (sales and support : were both are using shared module)

my router-outlet yet is not recognized by my main component, Why ?

exception in router-outlet

For my basic understanding of angular routing and modules handling:

Importing a module is importing it exported components : in my case the RouterModule(@angular/router) is exported in RouterRoutingModule, which is imported in the AppModule via my RouterModule(./router/router.module), so all it exported component should be imported

1
Your RouterModule isn't exporting anything. Try re-exporting the imported RouterRoutingModule by adding it to the exports array in RouterModulejulianobrasil
@jpavel I thought that importing module A that imports module B will make B exported component available, this happen to be falsy, thanksMohammed Housseyn Taleb
That's how NgModule works. When you're importing a module in another one, you'll have in the latter just what you're exporting in the former.julianobrasil
@MohammedHousseynTaleb have you use <router-outlet></router-outlet> in your main component or appComponentAbhishek

1 Answers

2
votes

I'm not sure why you have two modules called Router and RouterRouting, but if you do, you need to re-export the router in the intermediary module

@NgModule({
  declarations: [],
  imports: [
    CommonModule,
    RouterRoutingModule
  ],
export: [ RouterRoutingModule ]
})
export class RouterModule { }