Class LocalStorageService
- Namespace
- NuvTools.AspNetCore.Blazor.JSInterop
- Assembly
- NuvTools.AspNetCore.Blazor.dll
Provides JavaScript interop functionality for browser localStorage operations.
public sealed class LocalStorageService : ILocalStorageService
- Inheritance
-
LocalStorageService
- Implements
- Inherited Members
Remarks
Initializes a new instance of the LocalStorageService class.
Constructors
LocalStorageService(IJSRuntime)
Provides JavaScript interop functionality for browser localStorage operations.
public LocalStorageService(IJSRuntime jsRuntime)
Parameters
jsRuntimeIJSRuntimeThe JavaScript runtime for interop operations.
Remarks
Initializes a new instance of the LocalStorageService class.
Methods
ClearAsync()
Clears all items from localStorage.
public ValueTask ClearAsync()
Returns
ContainKeyAsync(string)
Checks if a key exists in localStorage.
public ValueTask<bool> ContainKeyAsync(string key)
Parameters
keystringThe key to check for existence.
Returns
- ValueTask<bool>
A ValueTask<TResult> representing the asynchronous operation, containing true if the key exists, otherwise false.
GetItemAsStringAsync(string)
Gets an item from localStorage as a raw string.
public ValueTask<string?> GetItemAsStringAsync(string key)
Parameters
keystringThe key of the item to retrieve.
Returns
- ValueTask<string>
A ValueTask<TResult> representing the asynchronous operation, containing the stored string value or null if not found.
GetItemAsync<T>(string)
Gets an item from localStorage and deserializes it from JSON.
public ValueTask<T?> GetItemAsync<T>(string key)
Parameters
keystringThe key of the item to retrieve.
Returns
- ValueTask<T>
A ValueTask<TResult> representing the asynchronous operation, containing the deserialized value or default if not found.
Type Parameters
TThe type to deserialize the stored value to.
KeyAsync(int)
Gets the key at the specified index in localStorage.
public ValueTask<string?> KeyAsync(int index)
Parameters
indexintThe zero-based index of the key to retrieve.
Returns
- ValueTask<string>
A ValueTask<TResult> representing the asynchronous operation, containing the key at the specified index or null if the index is out of range.
LengthAsync()
Gets the number of items stored in localStorage.
public ValueTask<int> LengthAsync()
Returns
- ValueTask<int>
A ValueTask<TResult> representing the asynchronous operation, containing the number of stored items.
RemoveItemAsync(string)
Removes an item from localStorage.
public ValueTask RemoveItemAsync(string key)
Parameters
keystringThe key of the item to remove.
Returns
SetItemAsStringAsync(string, string)
Sets an item in localStorage as a raw string.
public ValueTask SetItemAsStringAsync(string key, string value)
Parameters
Returns
SetItemAsync<T>(string, T)
Sets an item in localStorage by serializing the value to JSON.
public ValueTask SetItemAsync<T>(string key, T value)
Parameters
keystringThe key of the item to set.
valueTThe value to serialize and store.
Returns
Type Parameters
TThe type of the value to store.