0

I have a core module that has a layout component which includes a toolbar module and sidenav module. I then have a map module I want to be a child of the layout module so the html should look something like:

<root>
  <layout>
    <sidenav></sidenav>
    <toolbar></toolbar>
    <map></map>
  </layout>
</root>

I have tested all modules separately and they work but when I look at the html, it looks like this instead of above:

<root>
  <map></map>
</root>

It looks like the map module just overrides everything. If I change up the routes and only have the layout, I can see the toolbar and sidenav work and look like this:

<root>
  <layout>
    <sidenav></sidenav>
    <toolbar></toolbar>
  </layout>
</root>

So it doesnt seem to be an error in any of the modules themselves. The nested routing just doesnt seem to be working

the app-routing.module.ts:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { LayoutComponent } from './core/layout/layout.component';

const routes: Routes = [{
   path: '',
   component: LayoutComponent,
   children: [{
    path: '',
    loadChildren: 'app/map-feature/map.module#MapModule',
    pathMatch: 'full'
   }]
 }];

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

core.module.ts:

import { NgModule, Optional, SkipSelf  } from '@angular/core';
import { LayoutModule } from './layout/layout.module';

@NgModule({
 imports: [
 // Layout Module (Sidenav, Toolbar)
 LayoutModule
 ],
 declarations: []
 })

 export class CoreModule {
 constructor(@Optional() @SkipSelf() parentModule: CoreModule) {
 if (parentModule) {
  throw new Error(
    'CoreModule is already loaded. Imported in AppModule only.');
 }
 }
 }

layout.module.ts:

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

import { MaterialModule } from '@app/shared/material.module';
import { ToolbarModule } from '../toolbar/toolbar.module';
import { SidenavModule } from '../sidenav/sidenav.module';
import { LayoutComponent } from './layout.component';

@NgModule({
  imports: [
    CommonModule,
    RouterModule,
    MaterialModule,
    SidenavModule,
    ToolbarModule
 ],
 declarations: [LayoutComponent]
 })
 export class LayoutModule { }

map-routing.module.ts:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { MapComponent } from './map.component';

const routes: Routes = [
 {
   path: '',
   component: MapComponent
 }
];

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

map.module.ts:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { LeafletModule } from '@asymmetrik/ngx-leaflet';

import { MaterialModule } from '@app/shared/material.module';
import { MapRoutingModule } from './map-routing.module';
import { MapComponent } from './map.component';

@NgModule({
  imports: [
    CommonModule,
    MapRoutingModule,
    MaterialModule,
    LeafletModule.forRoot()
  ],
  declarations: [MapComponent]
   })
   export class MapModule { }

component.html

 <router-outlet></router-outlet>

layer.component.html

<mat-drawer-container class="layout-container">

 <mat-drawer-content class="drawer-content">

<div class="wrapper" fxFlex="grow" fxLayout="row">

  <!-- SIDENAV -->
  <sidenav class="sidenav"></sidenav>
  <!-- END SIDENAV -->

  <div class="content-wrapper" fxFlex="grow" fxLayout="column">
    <!-- TOOLBAR -->
    <toolbar></toolbar>
    <!-- END TOOLBAR -->

    <!-- CONTENT -->
    <div class="main-container" fxFlex="grow">
      <router-outlet></router-outlet>
    </div>
    <!-- END CONTENT -->

  </div>

</div>

user3214545
  • 2,103
  • 3
  • 20
  • 26
  • showing us the component.html will make it much much easier – Joshua Chan May 13 '18 at 08:07
  • Just added it above, just has the router-outlet in it. The map.component.html is just a div:
    – user3214545 May 13 '18 at 08:35
  • you only have one in your root component? You should have a router-outlet in your layout.component – Joshua Chan May 13 '18 at 08:44
  • sorry - I do have it in my layer component, not the map.component as thats a child – user3214545 May 13 '18 at 08:47
  • As a guess, your map.module.ts should have forChild rather than the forRoot. LeafletModule.forRoot() change it to LeafletModule.forChild() – Rohit.007 May 13 '18 at 08:47
  • Tried the LeafletModule.forChild(), no joy, tried taking leaflet out and no joy either – user3214545 May 13 '18 at 08:50
  • just to be sure, you have imported AppRoutingModule into CoreModule right? Also, could try removing the RoutingModule for MapModule and see if it works first. – Joshua Chan May 13 '18 at 09:18
  • I have imported the RouterModule but not the AppRoutingModule, as that is only imported into the app.module and the app module imports the Core Module. I did try to add it just in case but it throws an error. Removing the MapRoutingModule works if I change the app-routing.module (it cant be a child of Layout Component anymore). It just routes to the map page. If I leave it as a child, it throws an error. But I dont want it to just route to the map page as I want teh toolbar and sidenav to be there – user3214545 May 13 '18 at 09:36

0 Answers0