Blazored.LocalStorage包是:
一个库,用于访问Blazor应用程序中的本地存储
将服务注入组件很容易:
@inject Blazored.LocalStorage.ILocalStorageService localStorage
如果我们想把它注入后面的代码中:
[Inject]
private ILocalStorageService localStorage { get; set; }
但假设我想把它注入另一个服务(比如说集中控制(:
public class StorageManagement
{
public StorageManagement(LocalStorageService localStorage)
{
//How to initialize it here?
}
}
我不知道如何初始化StorageManagement
构造函数中的服务实例,也不知道如何在Program.cs:中设置StorageManagement
构造函数的参数
builder.Services.AddSingleton(e => new StorageManagement(//?));
只有builder.Services.AddBlazoredLocalStorage();
或builder.Services.AddBlazoredLocalStorage(config => config.JsonSerializerOptions.WriteIndented = true);
,正如README中所解释的那样
然后是builder.Services.AddScoped<StorageManagement>();
或builder.Services.AddScoped(p => new StorageManagement(p.GetRequiredSerice<ILocalStorageService>()));
但是您的服务应该采用ILocalStorageService
而不是LocalStorageService
实例:
public class StorageManagement
{
public StorageManagement(ILocalStorageService localStorage)
{
//How to initialize it here?
}
}
您的构造函数中没有使用接口ILocalStorageServer
,它应该是
public class StorageManagement
{
private readonly ILocalStorageService LocalStorage;
public StorageManagement(ILocalStorageService localStorage)
{
LocalStorage = localStorage;
}
}