Overview
TherouteSplit utility function parses Angular route strings and provides helper methods to work with route parameters. It breaks down a route into its component parts and identifies dynamic parameters, making it easier to generate routes programmatically.
Function Signature
SplitRoute Interface
Parameters
An Angular route string, potentially containing dynamic parameters prefixed with
: (e.g., /blog/:slug or /user/:id/posts/:postId)Return Value
Returns an object with three properties:An array of all route segments with their positions. Each segment includes:
part: The route segment (including:prefix for parameters)position: The zero-based index in the route path
An array of only the dynamic parameter segments with:
part: The parameter name (without the:prefix)position: The zero-based index where the parameter appears in the route
A function that accepts parameter values and generates a complete route path. Pass values in the order they appear in the route.Signature:
(...data: string[]) => stringUsage Examples
Basic Route Parsing
Multiple Parameters
Handling Empty Routes
In a Router Plugin
Building Route Variants
How It Works
Route Parsing
The function splits the route string by
/ and creates a SplitRoute object for each segment with its position.Parameter Identification
Segments starting with
: are identified as parameters. The : prefix is removed from the parameter name in the params array.Edge Cases
Null or undefined routes
Null or undefined routes
If the route is
null or undefined, the function returns:Routes without parameters
Routes without parameters
Static routes (e.g.,
/about/team) work perfectly fine. The params array will be empty, and createPath will return the original route.Trailing slashes
Trailing slashes
The function preserves the structure but doesn’t add trailing slashes. If your route has a trailing slash, it will be maintained.
Common Use Cases
Route Generation
Generate concrete routes from parameterized route templates in router plugins
Route Analysis
Analyze routes to understand their structure and parameter requirements
Dynamic Sitemap
Build sitemaps by combining route templates with actual data
Testing
Create test routes programmatically for plugin development
Best Practices
Related
Route Discovery
Understand how Scully discovers and processes routes
Router Plugins
Learn about creating custom router plugins
Handled Routes
See how routes are handled and transformed
Source Code
Location:libs/scully/src/lib/utils/routeSplit.ts:5
