Blazor全局单例对象-使用依赖注入?

时间:2020-04-15 16:25:10

标签: singleton blazor

我有一个单一的CUSTOMER对象,它需要从Blazor应用程序的所有部分访问/访问,从MainLayout到NavMenu再到razor组件。如何实现全局单例对象?

我试图像这样

Startup.cs 中使用DI
services.AddSingleton<ICustomer, Customer>();

然后在 MainLayout

@inject Customer cust

然后设置一些属性。

然后在 CustomerPage

  @inject Customer cust

但是CUSTOMERPAGE中的值是空白

我想念什么?我需要在整个应用程序中保留该对象。

1 个答案:

答案 0 :(得分:4)

您应该通过以下界面进行注入:

@inject ICustomer cust

或自行注册课程:

services.AddSingleton<Customer, Customer>();

@inject Customer cust
相关问题