【问题标题】:Pass properties to a Blazor Component将属性传递给 Blazor 组件
【发布时间】:2021-02-07 17:32:15
【问题描述】:

当我了解 Reactjs 中的组件时,我可以尝试这样的事情

<Component {...properties} /> 现在,我学习 Blazor。如何将属性传递给某些组件??

【问题讨论】:

    标签: c blazor blazor-server-side


    【解决方案1】:

    【讨论】:

    • 考虑从这些链接中提供提取版本,而不是链接网站,但它们与问题相关。
    • 对不起,我知道这一点,但我想要的是:当在 blazor 和 ParentComponent.razor 我有这样的东西
      的属性包括 Title 和 Description。在 js 中,我可以轻松做到,但在 Blazor 中我不知道如何
    • 请参阅@Douglas-Riddle 的答案以获取示例。
    【解决方案2】:

    您在组件定义中使用[Parameter]。例如,如果您有这样的 Blazor 组件

    // BlazorComponent.razor
    <div class="@Class">
      @ChildContent
    </div>
    
    @code {
      [Parameter] public string Class { get; set; }
      [Parameter] public RenderFragment ChildContent { get; set;}
    }
    

    你可以这样消费

    <BlazorComponent Class="some-class">
      <p>Some Content</p>
    </BlazorComponent>
    

    这会生成类似的html

    <div class="some-class">
      <p>Some Content</p>
    </div>
    

    【讨论】:

      猜你喜欢
      • 2021-01-30
      • 1970-01-01
      • 2021-01-18
      • 2016-08-31
      • 1970-01-01
      • 2023-03-20
      • 2018-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多