--- description: Complete reference for route path expression syntax, including sibling routes, child routes, viewports, and parameters. --- # Route Expression Syntax The Aurelia router uses a powerful expression syntax for defining navigation paths. This reference documents all available syntax elements and their combinations. ## Quick Reference | Syntax | Meaning | Example | |--------|---------|---------| | `component` | Navigate to component | `products` | | `/component` | Absolute navigation | `/dashboard` | | `a/b` | Child route (b is child of a) | `users/profile` | | `a+b` | Sibling routes (a and b in parallel) | `list+detail` | | `+a` | Append to current routes | `+sidebar` | | `(a+b)` | Group for precedence | `a/(b+c)` | | `@viewport` | Target specific viewport | `products@main` | | `(params)` | Inline parameters | `product(id=42)` | | `:param` | Path parameter (single segment) | `users/:id` | | `*param` | Wildcard parameter (multiple segments) | `files/*path` | | `?query` | Query string | `search?q=aurelia` | | `#fragment` | URL fragment | `docs#section-1` | ## Grammar The formal grammar for route expressions: ``` route := [ '/' ] composite_segment [ '?' query ] [ '#' fragment ] composite_segment := [ '+' ] scoped_segment { '+' scoped_segment } scoped_segment := segment_group [ '/' scoped_segment ] segment_group := '(' composite_segment ')' | segment segment := component [ '@' viewport ] [ '!' ] component := component_name [ '(' parameter_list ')' ] | ':' parameter_name [ '(' parameter_list ')' ] | '*' parameter_name [ '(' parameter_list ')' ] | './' | '../' viewport := viewport_name parameter_list := parameter { ',' parameter } parameter := parameter_name '=' parameter_value | parameter_value query := key '=' value { '&' key '=' value } fragment := text ``` ## Basic Navigation ### Component Names Navigate to a component by its registered name: ```html Products About ``` ```typescript router.load('products'); router.load('about'); ``` ### Absolute vs Relative Paths Paths starting with `/` are absolute (from root). Paths without `/` are relative to current context. ```html Dashboard Settings ``` ```typescript // Absolute navigation router.load('/dashboard'); // Relative navigation (respects current context) router.load('settings', { context: this.routeContext }); ``` ## Child Routes (Hierarchical Navigation) Use `/` to navigate to child routes: ```html User Profile Edit User 42 ``` ```typescript router.load('users/profile'); router.load('admin/users/42/edit'); ``` This creates a hierarchical route tree where each segment is a child of the previous one. ## Sibling Routes (Parallel Navigation) Use `+` to load multiple components in parallel viewports: ```html Show Both Full Layout ``` ```typescript router.load('list+detail'); // Equivalent object syntax: router.load([ { component: 'list', viewport: 'left' }, { component: 'detail', viewport: 'right' } ]); ``` ### Append to Current Routes Start with `+` to add to existing routes without replacing: ```html Show Sidebar Show Panels ``` ## Grouping with Parentheses Parentheses control precedence when combining `/` and `+`: Consider this viewport structure: ``` viewport-a ├── viewport-a1 └── viewport-a2 viewport-b └── viewport-b1 ``` Without grouping, `a/a1+a2+b/b1` would be parsed as: ``` a └── a1 a2 (sibling of a, not child) b └── b1 ``` With grouping, `a/(a1+a2)+b/b1` correctly parses as: ``` a ├── a1 └── a2 b └── b1 ``` ```html Complex Layout Full App ``` ## Viewport Targeting Use `@` to specify which viewport should receive the component: ```html Products Products with Cart ``` ```typescript router.load('products@main'); // Equivalent object syntax: router.load({ component: 'products', viewport: 'main' }); ``` ### Combining with Child Routes ```html Product Details Dashboard View ``` ## Inline Parameters Pass parameters directly in the path using parentheses: ```html Product 42 Red Product 42 Product 42 ``` ```typescript router.load('product(id=42)'); // Equivalent object syntax: router.load({ component: 'product', params: { id: '42' } }); ``` ### With Viewport Targeting ```html Product in Main Product and Cart ``` ## Dynamic Path Parameters ### Single-Segment Parameters (`:param`) Matches exactly one path segment: ```typescript // Route configuration @route({ routes: [ { path: 'users/:id', component: UserDetail }, { path: 'users/:id/posts/:postId', component: PostDetail } ] }) ``` ```html User 123 Post 456 ``` ### Multi-Segment Parameters (`*param`) Matches one or more path segments (wildcard): ```typescript // Route configuration @route({ routes: [ { path: 'files/*path', component: FileViewer } ] }) ``` ```html View Readme ``` ### Optional Parameters (`:param?`) Parameters can be made optional in route configuration: ```typescript @route({ routes: [ { path: 'search/:query?', component: Search } ] }) ``` ```html Search (no query) Search for Aurelia ``` ### Constrained Parameters Use regex constraints in route configuration: ```typescript @route({ routes: [ // Only match numeric IDs { path: 'orders/:id{{^\\d+$}}', component: OrderDetail } ] }) ``` ## Query Strings Append query parameters with `?`: ```html Search Results Electronics ``` ```typescript // Using path string router.load('search?q=aurelia&page=2'); // Using options (preferred) router.load('search', { queryParams: { q: 'aurelia', page: '2' } }); ``` ### Combining with Other Syntax ```html User's Posts List and Detail Books ``` ## URL Fragments Append hash fragments with `#`: ```html Jump to Installation Article Comments ``` ```typescript router.load('docs', { fragment: 'installation' }); ``` ### Combining Query and Fragment ```html API Reference for v2 ``` ```typescript router.load('docs', { queryParams: { version: '2' }, fragment: 'api-reference' }); ``` ## Relative Navigation ### Current Directory (`./`) Navigate relative to current route: ```html Current Route Sibling Component ``` ### Parent Directory (`../`) Navigate up one level: ```html Parent Route Parent's Sibling Grandparent ``` ```typescript // Navigate to parent's sibling router.load('../settings', { context: this.routeContext }); ``` ## Scope Modifier (`!`) The `!` suffix marks a segment as "unscoped", preventing child routes from inheriting its context: ```html Modal doesn't scope content ``` This is an advanced feature used in specific multi-viewport scenarios. ## Reserved Characters The following characters have special meaning and cannot be used literally in component or viewport names: | Character | Meaning | |-----------|---------| | `/` | Child route separator | | `+` | Sibling route separator | | `@` | Viewport separator | | `(` `)` | Grouping / parameters | | `?` | Query string start | | `#` | Fragment start | | `=` | Parameter value assignment | | `,` | Parameter separator | | `!` | Scope modifier | To use these characters in parameter values, URL-encode them: - `/` → `%2F` - `+` → `%2B` - `@` → `%40` ## Complete Examples ### E-commerce Application ```html Electronics by Price Product 123 Shipping Payment Review Order ``` ### Dashboard Application ```html Full Dashboard Q1 2024 Sales Report Open Settings ``` ### Documentation Site ```html Router Base Path IRouter Interface View App Component ``` ## Programmatic Equivalents Every path expression can be written as an object or array: ```typescript // Path: products(id=42)@main/details router.load({ component: 'products', params: { id: '42' }, viewport: 'main', children: [{ component: 'details' }] }); // Path: list+detail router.load([ { component: 'list' }, { component: 'detail' } ]); // Path: dashboard/(chart+table)@main router.load({ component: 'dashboard', viewport: 'main', children: [ { component: 'chart' }, { component: 'table' } ] }); ``` ## Tips and Best Practices 1. **Use route IDs for complex paths**: Instead of hardcoding paths, use route IDs with the `load` attribute: ```html View ``` 2. **Prefer object syntax for dynamic navigation**: It's more readable and type-safe: ```typescript router.load({ component: ProductDetail, params: { id } }); ``` 3. **Use relative navigation in nested routes**: Keeps child routes decoupled from parents: ```typescript router.load('../settings', { context: this.routeContext }); ``` 4. **Validate parameters in `canLoad`**: Don't rely solely on path constraints: ```typescript canLoad(params) { if (!params.id || isNaN(Number(params.id))) { return 'not-found'; } return true; } ```