我有一个单一的CUSTOMER对象,它需要从Blazor应用程序的所有部分访问/访问,从MainLayout到NavMenu再到razor组件。如何实现全局单例对象?
我试图像这样
在 Startup.cs 中使用DIservices.AddSingleton<ICustomer, Customer>();
然后在 MainLayout
中@inject Customer cust
然后设置一些属性。
然后在 CustomerPage
中 @inject Customer cust
但是CUSTOMERPAGE中的值是空白
我想念什么?我需要在整个应用程序中保留该对象。
答案 0 :(得分:4)
您应该通过以下界面进行注入:
@inject ICustomer cust
或自行注册课程:
services.AddSingleton<Customer, Customer>();
@inject Customer cust