我有 13 个参数,当用户登录网站时将如下设置:
`ProtectedSessionStore.SetAsync("iduser", iduser);
ProtectedSessionStore.SetAsync("unitid", unitid);
ProtectedSessionStore.SetAsync("branchid", branchid);
ProtectedSessionStore.SetAsync("organid", organid);
ProtectedSessionStore.SetAsync("defuser", defuser);
ProtectedSessionStore.SetAsync("defbranch", defbranch);
ProtectedSessionStore.SetAsync("defunit", defunit);
ProtectedSessionStore.SetAsync("defproduct", defproduct);
ProtectedSessionStore.SetAsync("defpointofbody", defpointofbody);
ProtectedSessionStore.SetAsync("defdegree", defdegree);
ProtectedSessionStore.SetAsync("defvisit", defvisit);
ProtectedSessionStore.SetAsync("defpatient", defpatient);
ProtectedSessionStore.SetAsync("report", report);`
登录后,我想在 2 个剃刀页面中保留这些值。其中一页是我的导航菜单布局,另一页是主菜单。 所以在两者中我都有这个代码:
var result = await ProtectedSessionStore.GetAsync<int>("iduser");
iduser = result.Success ? result.Value : 0;
result = await ProtectedSessionStore.GetAsync<int>("unitid");
unitid = result.Success ? result.Value : 0;
result = await ProtectedSessionStore.GetAsync<int>("branchid");
branchid = result.Success ? result.Value : 0;
result = await ProtectedSessionStore.GetAsync<int>("organid");
organid = result.Success ? result.Value : 0;
var result1 = await ProtectedSessionStore.GetAsync<bool>("defuser");
defuser = result1.Success ? result1.Value : false;
result1 = await ProtectedSessionStore.GetAsync<bool>("defbranch");
defbranch = result1.Success ? result1.Value : false;
result1 = await ProtectedSessionStore.GetAsync<bool>("defunit");
defunit = result1.Success ? result1.Value : false;
result1 = await ProtectedSessionStore.GetAsync<bool>("defvisit");
defvisit = result1.Success ? result1.Value : false;
result1 = await ProtectedSessionStore.GetAsync<bool>("defdegree");
defdegree = result1.Success ? result1.Value : false;
result1 = await ProtectedSessionStore.GetAsync<bool>("defpointofbody");
defpointofbody = result1.Success ? result1.Value : false;
result1 = await ProtectedSessionStore.GetAsync<bool>("defpatient");
defpatient = result1.Success ? result1.Value : false;
result1 = await ProtectedSessionStore.GetAsync<bool>("defproduct");
defproduct = result1.Success ? result1.Value : false;
result1 = await ProtectedSessionStore.GetAsync<bool>("report");
report = result1.Success ? result1.Value : false;
问题是加载这个参数非常慢。我怎样才能加快这个过程?
以下示例展示了如何构建与存储一起使用的服务。
服务:
public class DataService
{
private ProtectedLocalStorage _storage;
public MyData Data { get; private set; } = new($"Default values set at {DateTime.Now.ToLongTimeString()}");
public event EventHandler<MyData>? DataChanged;
public const string StorageName = "mydata";
public DataService(ProtectedLocalStorage protectedLocalStorage)
{
_storage = protectedLocalStorage;
}
public async ValueTask GetAsync()
{
var result = await _storage.GetAsync<MyData>(StorageName);
if (result.Success)
{
this.Data = result.Value ?? new($"Default values set at {DateTime.Now.ToLongTimeString()}");
this.DataChanged?.Invoke(this, this.Data);
}
}
public async ValueTask SetAsync()
{
await _storage.SetAsync(StorageName, this.Data);
this.DataChanged?.Invoke(this, this.Data);
}
public async ValueTask SetValueAsync(string value)
{
this.Data = this.Data with {Value = value };
await SetAsync();
}
}
在DI注册:
// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
builder.Services.AddScoped<DataService>();
主布局。 添加到此处是为了尝试获取 SPA 首次加载时的值。
@inherits LayoutComponentBase
@inject DataService dataService
//...
@code {
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
await dataService.GetAsync();
}
}
与服务的演示交互主页:
@page "/"
@inject DataService DataService
@implements IDisposable
<PageTitle>Home</PageTitle>
<h1>Hello, world!</h1>
<div class="alert alert-primary">
@this.DataService.Data.Value
</div>
<div class="m-2">
<button class="btn btn-primary" @onclick="this.OnClickAsync">Update</button>
</div>
@code {
protected override void OnInitialized()
{
this.DataService.DataChanged += this.OnDataChanged;
}
private async Task OnClickAsync()
{
await DataService.SetValueAsync($"Last Updated at {DateTime.Now.ToLongTimeString()}");
}
private void OnDataChanged(object? sender, MyData data)
{
this.InvokeAsync(this.StateHasChanged);
}
public void Dispose()
{
this.DataService.DataChanged -= this.OnDataChanged;
}
}
如果您想更新 UI 以显示信息,请注意需要该事件。