Question: Does exist a best practice for defining single-page applications' routes?
In Angular projects functionality is generally separated in lazy-loaded modules, then the routes are configured in both AppRoutingModule
and the lazy loaded modules.
Let's say the app will manage catalogs, for example: products. The routes could be configured like this:
Option 1:
/products
/products/create
/products/:id
/products/:id/edit
It works, but looks a bit messy and there is some ambiguity between /products/:id
and /products/create
because the parameter :id
can match the string "create". Example code:
app-routing.module.ts:
const routes: Routes = [
{
path: '',
children: [
{ path: 'products', loadChildren: 'app/products/products.module#ProductsModule' },
]
}
];
products-routing.module.ts
const routes: Routes = [
{ path: '', component: ListProductsComponent },
{ path: 'create', component: CreateProductComponent },
{ path: ':id', component: ViewProductComponent },
{ path: ':id/edit', component: EditProductComponent },
];
Option 2
/products
/products/create
/product/:id
(notice "product" is singular)/product/:id/edit
(notice "product" is singular)There is no ambiguity, but the configuration becomes messier:
app-routing.module.ts:
const routes: Routes = [
{
path: '',
children: [
// Empty path. It works as long as the ProductsModule has no empty paths. You can define more lazy modules like this.
{ path: '', loadChildren: 'app/products/products.module#ProductsModule' },
]
}
];
products-routing.module.ts
const routes: Routes = [
{ path: 'products', component: ListProductsComponent },
{ path: 'products/create', component: CreateProductComponent },
{ path: 'product/:id', component: ViewProductComponent },
{ path: 'product/:id/edit', component: EditProductComponent },
];
As you can see, when defining routes you have to consider the structure of the lazy modules and the "beauty" of the URLs.
What is the best practice for defining routes, specially for CRUD operations? Is there a good name convention?
Overview of Angular RoutingEntering a URL in the address bar and the browser navigates to a corresponding page. Clicking links on the page and the browser navigates to a new page. Clicking the browser's back and forward buttons and the browser navigates backward and forward through the history of pages.
Angular Router supports multiple outlets in the same application. A component has one associated primary route and can have auxiliary routes. Auxiliary routes enable developers to navigate multiple routes at the same time.
CRUD stands for the four functions create, read, update, and delete in computer programming. They are used to implement persistent storage applications and relational database applications: Create: A create operation allows you to add a new record to a table.
The only conventions/best-practices regarding Angular that I'm aware of come from the official Style guide, it doesn't mention anything about routes. But definitely a recommended/good read.
Personally I would put 'create' and 'edit' into another module with a different root url.
admin/product/list
admin/product/edit/:id
admin/product/create
Else I'd use a slightly modified version of your Option 1.
List: /products
Create: /products/create
Update: /products/edit/:id
Read: /products/:id
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With