blazor - 如何在 blazor 组件类中为我自己的服务使用依赖注入
问题描述
如何在 blazor 组件类中为我自己的服务使用依赖注入?
组件类:
[Inject]
public HttpContentFormatter IHttpContentFormatter {
get;
set;
}
解决方案
假设您的应用程序是客户端 Blazor,您应该将对象添加到 DI 容器,如下所示:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IHttpContentFormatter>();
}
public void Configure(IComponentsApplicationBuilder app)
{
app.AddComponent<App>("app");
}
}
在您的 Component 中,您可以像这样注入对象:
@inject IHttpContentFormatter HttpContentFormatter
推荐阅读
- docker - 如果注册表中不存在映像,则私有 docker registery 服务器从 docker hub 中提取
- javascript - textarea onchange 未在 php 脚本中触发
- python - Conda 安装不工作(回溯错误)
- tomcat - tomcat jar扫描的优点
- ios - 模拟器不可用 appcelerator studio
- php - 使用 Php 通过表单将项目添加到数组中
- javascript - 如何使上下文在单击时旋转
- c++ - 如果满足“if 语句”,则创建一个常量变量
- java - 在不使用 MQ 的情况下处理 Spring Cloud 微服务中的事务
- typescript - 从 TypeScript 编译的 JS 不能在浏览器中运行