The injectParams function is a versatile utility for injecting and transforming route parameters within an Angular application. It provides a reactive way to access and manipulate route parameters using Angular's signals-based architecture, making it easier to handle dynamic routing scenarios.
Examples
Example 1: Get All Route Parameters
When called without any arguments, returns a signal containing the entire params object:
import {injectParams} from'ngx-lift';
exportclassMyComponent {
// Returns a signal with the current route params
params = injectParams();
}
Example 2: Get Single Parameter
When a key is specified, returns the value of the corresponding parameter as a signal:
import {injectParams} from'ngx-lift';
import {Signal} from'@angular/core';
exportclassMyComponent {
// Returns a signal with the value of the id paramuserId: Signal<string | null> = injectParams('id');
}
Example 3: Transform Parameters
Transform parameters using a transform function:
import {injectParams} from'ngx-lift';
exportclassMyComponent {
// Returns a signal with the keys of the params
paramsKeys = injectParams((params) =>Object.keys(params));
// Pass a transform function directly
name = injectParams((params) => params['name'] asstring);
}
Example 4: Transform with Initial Value
Transform parameters with an initial value and type conversion:
import {injectParams} from'ngx-lift';
import {numberAttribute, Signal} from'@angular/core';
exportclassMyComponent {
// Returns a signal with the value of the id param, initialValue is 1id: Signal<number> = injectParams('id', {
transform: numberAttribute,
initialValue: 1,
});
}
When initialValue is provided as a boolean or number, transform function must be provided returning the same type.
Example 5: Complete Usage Example
Complete example showing various usage patterns:
import {injectParams, computedAsync} from'ngx-lift';
import {numberAttribute, Signal} from'@angular/core';
exportclassUserDetailComponent {
// Get all params
params = injectParams();
// Get single paramuserId: Signal<string | null> = injectParams('id');
// Transform with initial valueid: Signal<number> = injectParams('id', {
transform: numberAttribute,
initialValue: 1,
});
// Transform function
paramsKeys = injectParams((params) =>Object.keys(params));
// Use with computedAsync to fetch data when param changes
user = computedAsync(() =>this.userService.getUser(this.userId()));
}
API Reference
injectParams
Injects and transforms route parameters as reactive signals.
(Optional) When a string is provided, returns the value of that route parameter. When a function is provided, it's used as a transform function to process all route parameters.
If omitted, returns a signal containing the entire params object of the current route.
options?: InjectParamsOptions<T>
(Optional) Configuration object containing transform function and/or initialValue.
If initialValue is a boolean or number, transform function must be provided to convert the string parameter to the correct type.
Returns
A signal containing the route parameter value(s) or transformed result.
Usage Patterns
No arguments: Returns signal with all route parameters
String key: Returns signal with specific parameter value (or null if not present)
Transform function: Returns signal with transformed parameters
With options: Allows initial value and type conversion