Nested Routes
Nested routes allow child routes to be rendered within parent routes, creating a modular and reusable UI. Parent routes define shared components, while child routes are injected into specific outlets within these parent components.
Defining nested routes
Nested routes are created using the outlet parameter in the @Route annotation, which establishes a parent-child relationship. The outlet determines where the child component will be rendered within the parent route.
@Route
public class MainLayout extends Composite<AppLayout> {
public MainLayout() {
setHeader();
setDrawer();
}
}
@Route(outlet = MainLayout.class)
public class DashboardView extends Composite<Div> {
public DashboardView() {
getBoundComponent().add(new H1("Dashboard Content"));
}
}
@Route(outlet = DashboardView.class)
public class SettingsView extends Composite<Div> {
public SettingsView() {
getBoundComponent().add(new H1("Settings Content"));
}
}
In this example:
MainLayoutis a Layout Route.DashboardViewis a View Route nested insideMainLayout.SettingsViewis a View Route nested insideDashboardView.
When navigating to /dashboard/settings, the router:
- Renders
MainLayout. - Injects
DashboardViewinto the outlet ofMainLayout. - Finally, injects
SettingsViewinto the outlet ofDashboardView.
This hierarchical structure is reflected in the URL, where each segment represents a level in the component hierarchy:
- URL:
/dashboard/settings - Hierarchy:
MainLayout: Layout RouteDashboardView: View RouteSettingsView: View Route
This structure ensures consistent shared UI components (such as headers or navigation menus) while allowing the content within those layouts to change dynamically.