I am wondering how to properly bite the routing topic in my application.
How it's look now: app-routing.module.ts file:
const routes: Routes = [
{
path: '',
component: MainLayoutComponent,
children: [
{
path: '',
loadChildren: '../app/modules/home.module#HomeModule'
},
{
path: '',
loadChildren: '../app/modules/products.module#ProductsModule'
}
]
},
{
path: '**',
redirectTo: '/404'
}
];
I know its looking bad. Two same paths: ''. But in ProductsModule my routing looks like this:
products.routing.ts file:
const routes: Routes = [
{
path: 'products/:id',
component: ProductDetailsComponent
},
{
path: ':mainCategory',
component: ProductsComponent
},
{
path: ':mainCategory/:subCategory',
component: ProductsComponent
}
];
What im trying to do:
1.After go to: '/products/12331' load ProductDetailsComponent with id 12331.
Probably it's doesnt work because main module have path: ''.
Is the solution to this problem to separate the ProductDetailsComponent from the ProductsComponent?
In this way my app-routing.module.ts will looks:
const routes: Routes = [
{
path: '',
component: MainLayoutComponent,
children: [
{
path: '',
loadChildren: '../app/modules/home.module#HomeModule'
},
{
path: 'products',
loadChildren: '../app/modules/products.module#ProductsModule'
},
{
path: ':mainCategoryId',
loadChildren: '../app/modules/products.module#CategoriesModule' (adding new module with CategoriesComponent)
}
]
},
{
path: '**',
redirectTo: '/404'
}
];
Second problem is that i'm loading categories from backend. How do I solve the problem of routing to a category that does not exist? For example i have categories:
- Foods [Drinks,Meats, Pizza's]
- Clothes [Shoes, Jackets, Hats]
Now if im route to:
/Foods/Drinks - should load CategoriesComponent
/Foods/Shoes - should route to /404 but how to handle it? (categories not exists)
/randomName/Drinks - should route to /404
How i can resolve it? Maybe my question's are stupid but im learning angular :)