1

I am beginner with angular and I have the followings routes.

app.routing.module.ts

import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { FrameComponent } from './ui/frame/frame.component';
import { NotFoundComponent } from './common/not-found/not-found.component';

const routes = [
  {
    path: 'login',
    loadChildren: 'src/app/login/login.module#LoginModule'
  },
  { 
    path: 'dashboard',
    component: FrameComponent,
    loadChildren: 'src/app/dashboard/dashboard.module#DashboardModule'
  },

  {
      path: "**",
      component: NotFoundComponent,
  }

];

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

dashboard.routing.module.ts

import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { OverviewComponent } from '../overview/overview.component';


const routes = [
  { 
    path: '',
    children:[
      {
        path: 'overview',
        component: OverviewComponent,
        //outlet: 'dashboard-inside'
      }
    ]
  },
];

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

When navigating to /dashboard it loads the FrameComponent from the AppRoutingModule. But when navigating to /dashboard/overview it loads NotFoundComponent instead of OverviewComponent from second router.

I am still a beginner with Angular. What am I doing wrong?

2
  • Welcome. How exactly do you expect /dashboard/overview to navigate to OverviewComponent since that route (path) is not defined anywhere? Commented Dec 4, 2018 at 0:12
  • Isn't correctly defined in the children route of dashboard.routing.module.ts? Commented Dec 4, 2018 at 0:24

3 Answers 3

1

I think you didn't define your routes correctly

{ 
    path: 'dashboard',
    component: FrameComponent,
    loadChildren: 'src/app/dashboard/dashboard.module#DashboardModule'
  }

This piece of code doesn't load lazily - you are not loading the childern over here you are just loading the component FrameComponent so angular does it for you

If your FrameComponent is part of AppModule you can just remove the loadChildren from the path and the angular will do the same routing for you

If it is not the part of AppModule then try something like this

app-routing.module.ts

 { 
    path: 'dashboard',
    loadChildren: 'src/app/dashboard/dashboard.module#DashboardModule'
  }

Just load another module from the path and load the component you want from that module

dashboard-routing.module.ts

{ 
    path: '',
    component: FrameComponent,
    children:[
      {
        path: 'overview',
        component: OverviewComponent,
        //outlet: 'dashboard-inside'
      }
    ]
  }

Make sure you have declared the FrameComponemt inside the DashboardModule and that will make you to load the route you want

Now if the path is /dashboard angular will load the dashboard module and check for the path '' next to the /dashboard so it will load the FrameComponent then when you try to access path /dashboard/overview routing will load the child route and OverviewComponet will be loaded

Hope everything will work good - please feel free to reach me if you have any doubts - Happy coding :)

Sign up to request clarification or add additional context in comments.

Comments

1

You can remove component: FrameComponent from the dashboard route and move it into the dashboard routing module.

  { 
    path: 'dashboard',
    loadChildren: 'src/app/dashboard/dashboard.module#DashboardModule'
  },


  { 
    path: '',
    component: FrameComponent,
    children:[
      {
        path: 'overview',
        component: OverviewComponent,
      }
    ]
  },

And I guess you should import your modules in core one.

Comments

0

Your definition in dashboard.routing.module.ts is wrong.

Try this instead:

import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { OverviewComponent } from '../overview/overview.component';


const routes = [
  { 
    path: 'overview', // <-- should be in root.
    component: OverviewComponent,
  },
];

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.