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 and dynamic key support.
Injection Token: NGXTENSION_LOCAL_STORAGE
Section titled “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
Section titled “Function: provideLocalStorageImpl”- Parameters:
impl: A custom implementation of thelocalStorageinterface.
- Returns: An Angular provider object that uses the given implementation.
Type: LocalStorageOptions<T>
Section titled “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.clearOnKeyChange: If set to true, removes the value stored under the previous key from localStorage when the computed key changes. Applies only when a key-computation function is provided. Defaults totrue.
Function: injectLocalStorage
Section titled “Function: injectLocalStorage”- Parameters:
keyOrComputation: A string key or a computation function returning a key string under which data is stored. If a function is provided, the signal will reinitialize whenever the computed key changes.options: Configuration options of typeLocalStorageOptions.
- Returns: A reactive signal (
LocalStorageSignal<T>) withsetandupdatemethods representing the state of the local storage item. Updates to this signal are reflected in local storage (and emit astorageevent), and external storage events update the signal ifstorageSyncis enabled.
-
Injecting Custom Local Storage: Use
provideLocalStorageImplto override the default local storage implementation if necessary. -
Creating a Local Storage Signal: Use
injectLocalStorageto create a reactive local storage signal. Configure behavior withLocalStorageOptions.
Example
Section titled “Example”Here’s a basic example of using injectLocalStorage:
const username = injectLocalStorage<string>('username');
username.set('John Doe');username.update((name) => 'Guest ' + name);
effect(() => { console.log(username());});Use username in your component to get or set the username stored in local storage. The value might be undefined if no default value is provided.
Fallback value:
const username = injectLocalStorage<string>('username', { defaultValue: 'Guest',});Uses ‘Guest’ if ‘username’ is not in local storage.
Storage synchronization:
const username = injectLocalStorage<string>('username', { storageSync: true,});Changes reflect across browser tabs.
Dynamic key support:
const currentUserId = signal('user-1');const settings = injectLocalStorage(() => `settings-${currentUserId()}`, { defaultValue: { theme: 'light' },});Reinitializes when currentUserId() changes.