如何将对象从一个页面组件传递到.NET Blazor应用中的另一页组件?

发布于 2025-01-27 15:49:02 字数 229 浏览 3 评论 0原文

我有一个.NET Blazor Server应用程序,需要将对象从一个组件传递到另一个组件。这两个组件都是页面,这意味着它们具有带路线的@page指令。我知道如何使用级联值在常规的大火组件之间传递参数,但这与页面组件不起作用。我也知道如何在端点路由中传递参数。但是,我不想传递具有多个属性的对象,并且不确定如何最好地实现这一目标。

是否可以将对象作为端点路由参数传递?如果不是,那么在剃须乐组件上下文中实现这一目标的好方法是什么?

I have a .NET Blazor Server app and need to pass an object from one component to another. Both components are pages, meaning that they have @page directives with routes. I know how to use cascading values to pass a parameter between regular Blazor components, but this does not work with page components. I also know how to pass a parameter within an endpoint route. However, instead of a string or int I want to pass an object with multiple properties and am unsure how to best accomplish this.

Is it possible to pass an object as an endpoint route parameter? If not, what is a good way to accomplish this within a Razor components context?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晚雾 2025-02-03 15:49:02

使用依赖注入可能会为您解决此问题。

示例:

  1. 创建一个称为“ applicationservice”的类
  2. 创建该类中的接口,称为“ iapplicationservice”,

您可以在“ applicationservice.cs”文件中的applicationservice类中具有类似的内容

public interface IApplicationService
{
     public Task MsgBox(string value);
}
  1. ,然后继续并实现上面的接口成员。

您可以拥有这样的东西:

public async Task MsgBox(string value)
{
   await _JSRuntime.InvokeAsync<string>("alert", value);
}
  1. 在program.cs类中,您现在需要注册我们刚创建的“服务”。

您可以在_imports.razor中有类似的东西

builder.Services.AddTransient<IApplicationService, ApplicationService>();
builder.Services.AddScoped<ApplicationService>();
  1. ,可以注入类,以便页面可以访问:

    @Inject Applicationservice MainAppService;

  2. 现在在剃须刀组件中,您应该能够做类似的事情:

    等待mainappservice.msgbox(“这是一个消息框”);

这在我的WASM Blazor应用程序中起作用,希望它能在服务器端阐明

Using dependency injection would likely solve this issue for you.

Example:

  1. Create a class called "ApplicationService"
  2. Create an interface in that class called "IApplicationService"

You could have something like this

public interface IApplicationService
{
     public Task MsgBox(string value);
}
  1. In the ApplicationService class inside the "ApplicationService.cs" file, go ahead and implement the interface member above.

You could have something like this:

public async Task MsgBox(string value)
{
   await _JSRuntime.InvokeAsync<string>("alert", value);
}
  1. In the program.cs class, you need to now register that "service" we just created.

You could have something like this

builder.Services.AddTransient<IApplicationService, ApplicationService>();
builder.Services.AddScoped<ApplicationService>();
  1. In your _Imports.razor you can inject the class so that the pages have access to it:

    @inject ApplicationService MainAppService;

  2. Now in your razor components you should be able to do something like this:

    await MainAppService.MsgBox("This is a message box");

This works in my WASM blazor app, hope it sheds some light on the server side of things ????

久而酒知 2025-02-03 15:49:02

使用DI服务。创建一个班级以保持您的对象。将其添加为程序中的范围服务。在任何组件中使用它(页面只是具有页面属性的组件),但@inject

Use a DI service. Create a class to hold your object. Add it as a Scoped Service in Program. Use it in any component (pages are just components with a page attribute) though @inject.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文