blazor - 无法通过派生类的值设置具有基类类型的参数
问题描述
零件:
...
[Parameter]
public Base InitialModel {get;set;}
...
模型:
record Base() {}
record Derived():Base {}
用法:
<component type="typeof(...)" param-InitialModel="@(new Derived())" render-mode="WebAssemblyPrerendered" />
.....
结果:
System.InvalidOperationException: The parameter 'InitialModel with type 'Base' in assembly '...' could not be found.
预期的:
接受派生类代替基类
解决方案
我有一个类似的问题,我刚刚解决了它。
我必须在服务 DI 系统中注册课程。在 WASM 项目中编辑 program.cs 文件以将类添加到服务中
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
//builder.RootComponents.Add<App>("#app");
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.Services.AddScoped<MyViewModel, MyViewModel>(); // <= add this line
await builder.Build().RunAsync();
}
相关问题:How to pass ViewModels into Razor Components in .NET Core 3.1
推荐阅读
- c# - Bot framework V4 500 - 部署到 azure 时出现内部服务器错误(使用 Ngrok/本地)
- typescript - 如果语句中的简单类型缩小在 Typescript 中失败
- reactjs - 无法使用反应测试库模拟 useAsync
- javascript - 在反应数据网格中单击后单元格宽度正在增加
- r - 使用 tidymodels 验证模型假设
- python - ASP.NET_SessionId 访问被拒绝
- micro-frontend - centos server reload page 404 - 使用Single SPA的微前端应用
- netbeans - 如何显示从 rfid 到 jtextfield 的数据?我使用了 arduino r3 uno 和 netbeans ide 组合
- html - 非常奇怪的问题
- reactjs - 检查函数是否在 ReactJS 中触发