Table of Contents

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

jsRuntime IJSRuntime

The 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

ValueTask

A ValueTask representing the asynchronous operation.

ContainKeyAsync(string)

Checks if a key exists in localStorage.

public ValueTask<bool> ContainKeyAsync(string key)

Parameters

key string

The 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

key string

The 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

key string

The 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

T

The 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

index int

The 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

key string

The key of the item to remove.

Returns

ValueTask

A ValueTask representing the asynchronous operation.

SetItemAsStringAsync(string, string)

Sets an item in localStorage as a raw string.

public ValueTask SetItemAsStringAsync(string key, string value)

Parameters

key string

The key of the item to set.

value string

The string value to store.

Returns

ValueTask

A ValueTask representing the asynchronous operation.

SetItemAsync<T>(string, T)

Sets an item in localStorage by serializing the value to JSON.

public ValueTask SetItemAsync<T>(string key, T value)

Parameters

key string

The key of the item to set.

value T

The value to serialize and store.

Returns

ValueTask

A ValueTask representing the asynchronous operation.

Type Parameters

T

The type of the value to store.