代码示例:
@page "/counter"
Counter
Current count: @currentCount
@code {
private int currentCount = 0;
protected override async Task OnInitializedAsync()
{
// Check if state exists in local storage
if (await JS.InvokeAsync("localStorage.getItem", "counterExists"))
{
// Retrieve state from local storage
currentCount = await JS.InvokeAsync("localStorage.getItem", "counterState");
}
}
private async Task IncrementCount()
{
currentCount++;
// Save state to local storage
await JS.InvokeVoidAsync("localStorage.setItem", "counterState", currentCount);
await JS.InvokeVoidAsync("localStorage.setItem", "counterExists", true);
}
}