2

I want use lazy loading in your app. A have 2 modules: login and cars. Lazy loading is working fine, but route /add-car not working. Why? Path /add-car is not found and redirect to PageNotFoundComponent.

app.routing.module.ts

const appRoutes: Routes = [
    {
        path: '',
        pathMatch: 'full',
        redirectTo: 'login'
    },
    {
        path: 'cars',
        canLoad: [AuthCanLoadGuard],
        loadChildren: './cars/cars.module#CarsModule',
    },
    {
        path: 'user-account',
        component: UserAccountComponent,
        canActivate: [AuthGuardsService]
    },
    {
        path: '**',
        component: PageNotFoundComponent
    }
];

@NgModule({
    imports: [RouterModule.forRoot(appRoutes, {enableTracing: true})],
    exports: [RouterModule]
  })

cars.routing.module.ts

const carsRoutes: Routes = [
    {
        path: '',
        component: CarsComponent,
        children: [
            {
                path: '',
                component: CarsListComponent,
                resolve: { cars : CarsListResolve } // przeniesione z app.routing.module.ts
            },
            {
                path: ':id',
                component: CarsDetailsComponent,
                canDeactivate: [FormCanDeactivateGuard],
                resolve: { car: CarResolve }
            }
        ]
    },
    {
        path: '/add-car',
        component: AddCarComponent,

    }
];

@NgModule({
    imports: [RouterModule.forChild(carsRoutes)],
    exports: [RouterModule]
  })

3 Answers 3

2

Path cannot start with a slash. Adjust /add-car to add-car:

{
        path: 'add-car',
        component: AddCarComponent,

    }

And, as mentioned in the answer above, you'll also need the /cars context. As add-car is a child, it is only available under /cars, so path you'd use is in the browser is /cars/add-cars

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

Comments

1

You should be redirecting to cars/add-car since add-car is the part of cars.routing.module.ts; Also when you route, make sure to use { relativeTo: this.route }. Ex: this.router.navigate(['../add-car'], { relativeTo: this.route });

Comments

0

If I can get /car-add path I should add this path in app.routing.module.ts? It will be correct add addCarComponent outside cars module?

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.