injectLocalStorage
This injector provides a reactive local storage management system using Angular’s dependency injection and reactivity model. This API allows for easy integration and manipulation of local storage with real-time synchronization across browser tabs.
API
Injection Token: NGXTENSION_LOCAL_STORAGE
- Description: Token for the local storage service, allowing for custom implementations to be provided.
- Factory Default: Uses the browser’s
localStorage
.
Function: provideLocalStorageImpl
- Parameters:
impl
: A custom implementation of thelocalStorage
interface.
- Returns: An Angular provider object that uses the given implementation.
Type: LocalStorageOptions<T>
Options to configure the behavior of the local storage signal.
- Fields:
defaultValue
: The default value used if the key is not present in local storage. It can be a direct value or a function returning a value.storageSync
: If set totrue
, synchronizes data across browser tabs. Defaults totrue
.stringify
: A custom function to serialize stored data. Defaults toJSON.stringify
.parse
: A custom function to deserialize stored data. Defaults to a function handling JSON parsing with support forundefined
.injector
: Optional custom injector for dependency resolution.
Function: injectLocalStorage
- Parameters:
key
: The local storage key under which data is stored.options
: Configuration options of typeLocalStorageOptions
.
- Returns: A
WritableSignal
representing the state of the local storage item. Updates to this signal are reflected in local storage and vice versa ifstorageSync
is true.
Usage
-
Injecting Custom Local Storage: Use
provideLocalStorageImpl
to override the default local storage implementation if necessary. -
Creating a Local Storage Signal: Use
injectLocalStorage
to create a reactive local storage signal. Configure behavior withLocalStorageOptions
. -
Responding to Changes: Local storage changes reflect automatically in the signal if
storageSync
is enabled, and vice versa. Use the signal in your Angular components to reactively update the UI based on local storage data.
Example
Here’s a basic example of using injectLocalStorage
:
Fallback value can be provided using the defaultValue
option:
Storage synchronization can be enabled using the storageSync
option: