2

I got two modules in my application one is the app.module and another one is user.module which gets lazy-loaded. On the app.module I got a sign-in component, sign-up component and main component. The main.component is the component consisting of a navbar and a sidebar. I want every component inside user.module to load inside the main.component. But I'm getting error in console Error: Cannot match any routes. URL Segment: 'user/home'.

enter image description here

app.routing

import { MainComponent } from './main/main.component';
import { SignInComponent } from './sign-in/sign-in.component';
import { SignUpComponent } from './sign-up/sign-up.component';

const routes: Routes = [

      { path: 'sign-in', component: SignInComponent, pathMatch: 'full' },
      { path: '', redirectTo: 'sign-in', pathMatch: 'full' },
      { path: 'sign-up', component: SignUpComponent }
      {
        path: '',
        component: MainComponent,
        children: [
          {
            path: 'user',
            loadChildren: () => import('./user/user.module').then(m => m.UserModule)
          },
    
        ]
    
      },
      /* { path: '500', component: Error500Component },
      { path: '**', component: Error404Component }, */
    ];
    
    @NgModule({
      imports: [RouterModule.forRoot(routes)],
      exports: [RouterModule]
    })
    export class AppRoutingModule { }

user.routing

import { HomeComponent } from './home/home.component';
import { Routes, RouterModule } from '@angular/router';
import { FriendsComponent } from './friends/friends.component';

    const routes: Routes = [
  {
    path: '',
    children: [
      { path: 'home', component: HomeComponent },
      { path: 'friends', component: FriendsComponent }
    ]
  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class UserRoutingModule { }
3
  • You will need to share the error, and the full user-routing.ts code Commented Oct 4, 2020 at 11:23
  • updated..check now. In case you want anything more, let me know and I will create a stackblitz snippet Commented Oct 4, 2020 at 11:41
  • See my answer, your code looks ok so I can only assume you are missing the import of UserRoutingModule inside UserModule Commented Oct 4, 2020 at 12:13

2 Answers 2

0

You should import and export the UserRoutingModule in UserModule.

@NgModule({
  declarations: [..],
  imports: [UserRoutingModule],
  exports: [UserRoutingModule],
  providers: [..]
})
export class UserModule {}
Sign up to request clarification or add additional context in comments.

Comments

0

everything looks ok, have you specified router-outlet in your main.component.html ?

<router-outlet></router-outlet>

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.