简体   繁体   English

使用参数渲染 Blazor 子组件

[英]Render Blazor child component with parameters

I'm new to Blazor and I'm currently working on a Blazor Webassembly .net 5.0 application.我是 Blazor 的新手,我目前正在开发 Blazor Webassembly .net 5.0 应用程序。

I try to figure out the correct way to我试图找出正确的方法

  • render a child component渲染一个子组件
  • from a parent component从父组件
  • on button click (form submit)点击按钮(表单提交)
  • pass parameters from the parent component to => the child component将参数从父组件传递给 => 子组件

My current solution seems to work, but unfortunately it ends in an infinite rendering loop : I use the OnParametersSetAsync method in the child component to handle the data loading.我当前的解决方案似乎有效,但不幸的是它以无限渲染循环结束:我使用子组件中的OnParametersSetAsync方法来处理数据加载。

Side note: I use Telerik Blazor components, but it should have no impact.旁注:我使用 Telerik Blazor 组件,但应该没有影响。

  1. My parent component looks like this:我的父组件如下所示:
  • View (parent)查看(父)
// I want to submit a form to set a bool = true, and then to rend the child component - is that ok?
<EditForm OnValidSubmit="@(async () => await StartEverything())">
            <label for="OrderNumber">OrderNumber: </label>
            <TelerikTextBox @bind-Value="OrderNumber" Id="OrderNumber" />
            <TelerikButton ButtonType="@ButtonType.Submit">Start Everything</TelerikButton>
</EditForm>


@if (ShowChild)
{
    <MyChildComponent OrderNumber="OrderNumber"/>
}
else
{
    <div>Please enter an order number.</div>
}

  • Code Behind (parent)代码背后(父)
public class MyParentComponent : ComponentBase {

 protected int OrderNumber { get; set; }

 protected bool ShowChild { get; set; }

 protected async Task StartEverything()
 {
     if (OrderNumber > 0)
     {
         await Task.FromResult(ShowChild = true);
     }
 }
}
  1. My child component looks like this:我的子组件如下所示:
  • View (child)查看(儿童)
@if (Customer != null)
{
      <p>@Customer.CustomerName</p>
      <p>@Customer.AgencyName</p>
}
  • Code Behind (child)代码背后(儿童)
public class MyChildComponent : ComponentBase {

 // I need this Parameter sent from my parent component
 [Parameter]
 public int OrderNumber { get; set; }

 protected CustomerViewModel Customer { get; set; }

 protected override async Task OnParametersSetAsync()
 {
     var parameterForQuery = OrderNumber; // this should hold the value sent from the parent component

     // Load Customer ViewModel Data here - is this the correct event? What is the best approach?
 }
}
  • Item ViewModel项目视图模型
public class CustomerViewModel 
{
   public string CustomerName { get; set; }

   public string AgencyName { get; set; }  
}

Do you know how to correctly render a Child Component within a Parent Component and pass parameters from the Parent Component to the child component - then render the child component ONLY ON BUTTON CLICK (form submit, no infinite render loop)?您是否知道如何在父组件中正确渲染子组件并将参数从父组件传递给子组件 - 然后仅在按钮单击时渲染子组件(表单提交,没有无限渲染循环)?

Do you know how to solve this problem?你知道如何解决这个问题吗?

I recommend going through https://blazor-university.com .我建议通过https://blazor-university.com It's the site that kind of got me kick-started when I first started with Blazor.当我第一次开始使用 Blazor 时,这个网站让我开始了。

In regard to your question, I recommend the following:关于你的问题,我建议如下:

https://blazor-university.com/components/component-lifecycles/ https://blazor-university.com/components/component-lifecycles/

In particular, the following statement should prove useful in your case (from that link):特别是,以下语句在您的情况下应该证明是有用的(来自该链接):

OnInitialized / OnInitializedAsync OnInitialized / OnInitializedAsync

This method is only executed once when the component is first created.此方法仅在首次创建组件时执行一次。 If the parent changes the component's parameters at a later time, this method is skipped.如果父级稍后更改组件的参数,则跳过此方法。

It seems likely that simply changing which method you override will solve your problem, since OnParametersSetAsync behaves as you've described, and 'OnInitializedAsync' behaves as you want.似乎只需更改您覆盖的方法就可以解决您的问题,因为OnParametersSetAsync行为如您所描述的那样,并且“OnInitializedAsync”的行为如您所愿。 :D :D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM