如何在 blazor 中刷新页面后重置值?
How to after refresh page resetting value in blazor?
我想在 ISessionStorageService
用户转到 Dashboard 页面后正常工作时获取当前使用登录名,Dashboard 有布局页面中的 layout 获取当前用户登录的 SessionStorage
值,
布局页面
h1>@UserInfo.FullName</h1>
@Body
@code {
private LoginUser UserInfo = new LoginUser();
async Task UpdateUserInfo() => UserInfo = await SessionStorage.GetItemAsync<LoginUser>("LoginUser");
protected override async Task OnInitializedAsync()
{
await UpdateUserInfo();
}
}
登录页面设置值
await SessionStorage.SetItemAsync("LoginUser", loginUser);
注意:用户登录和页面之间的切换工作但是当我想要刷新页面时出现错误
InvalidOperationException: JavaScript interop calls cannot be issued at this time. This is because the component is being statically rendererd. When prerendering is enabled, JavaScript interop calls can only be performed during the OnAfterRenderAsync lifecycle method.
由于您正在使用预渲染,因此在此过程发生时您无法调用 JavaScript 方法。而是设置对 UpdateUserInfo 方法的调用
在 OnAfterRender(Async) 对中,像这样:
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if( firstRender )
{
await UpdateUserInfo();
InvokeAsync( () => StateHasChanged());
}
}
希望这对您有所帮助...
我想在 ISessionStorageService
用户转到 Dashboard 页面后正常工作时获取当前使用登录名,Dashboard 有布局页面中的 layout 获取当前用户登录的 SessionStorage
值,
布局页面
h1>@UserInfo.FullName</h1>
@Body
@code {
private LoginUser UserInfo = new LoginUser();
async Task UpdateUserInfo() => UserInfo = await SessionStorage.GetItemAsync<LoginUser>("LoginUser");
protected override async Task OnInitializedAsync()
{
await UpdateUserInfo();
}
}
登录页面设置值
await SessionStorage.SetItemAsync("LoginUser", loginUser);
注意:用户登录和页面之间的切换工作但是当我想要刷新页面时出现错误
InvalidOperationException: JavaScript interop calls cannot be issued at this time. This is because the component is being statically rendererd. When prerendering is enabled, JavaScript interop calls can only be performed during the OnAfterRenderAsync lifecycle method.
由于您正在使用预渲染,因此在此过程发生时您无法调用 JavaScript 方法。而是设置对 UpdateUserInfo 方法的调用 在 OnAfterRender(Async) 对中,像这样:
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if( firstRender )
{
await UpdateUserInfo();
InvokeAsync( () => StateHasChanged());
}
}
希望这对您有所帮助...