简体   繁体   English

如何分离从一个 razor 组件发送到另一个组件的 html 代码?

[英]How to separate html code that is sent from one razor component to another?

I'm calling a part of a table from a razor component to a component that can be viewed.我正在将表的一部分从 razor 组件调用到可以查看的组件。 But the the problem is that there is an audio element i want to separate so it can be called at a different place.但问题是我想分离一个音频元素,以便可以在不同的地方调用它。

Right now the audio element is included in the call in the loop.现在,音频元素包含在循环中的调用中。 Is there any way the audio element can be separated in the CallComponent.razor , such that it can be called at a different loaction in index.razor ?有没有什么方法可以在CallComponent.razor中分离音频元素,以便可以在index.razor中的不同位置调用它?

Here is some code:这是一些代码:

Index.razor

//I want to call the separated audio element here

...
<tbody>
        @foreach (var fileGroup in GroupedAndSorted)
        {
            <CallComponent fileGroup="fileGroup" /> 
        }
</tbody>
...

CallComponent.razor

<audio src="@audioUrl" controls>
</audio>

<tr>
    <td>
        <a @onclick="@(() => PlayAudio(Mp3.Url))"
                       class="link-primary"
                       role="button">
        @fileGroup.Key
        </a>
    </td>
</tr>
...

Here is one way, there are other ways involving various ways to handle application state, but this demonstrates possibly the simplest.这是一种方法,还有其他方法涉及处理应用程序 state 的各种方法,但这可能是最简单的演示。

Index.razor Index.razor

@childMarkup

<Component1 ExtraMarkup=@( em => childMarkup = em) />

@code 
{
    RenderFragment childMarkup;
}

Component1.razor组件1.razor


<h1>Component1</h1>

@code 
{
    [Parameter] public EventCallback<RenderFragment> ExtraMarkup { get;set;}

    protected override void OnInitialized()
    {
        ExtraMarkup.InvokeAsync( @<div>I am extra markup</div> );
    }
}

In this example, Component1 has a parameter that takes an EventCallback<RenderFragment> - in other words, you give it a method that accepts a RenderFragment and Component1 will call your method supplying it with some markup.在此示例中, Component1有一个采用EventCallback<RenderFragment>的参数 - 换句话说,您为其提供一个接受RenderFragment的方法, Component1将调用您的方法并为其提供一些标记。 Here we just store the incoming RenderFragment in a local field - which is then rendered in the parent markup.在这里,我们只是将传入的 RenderFragment 存储在本地字段中 - 然后在父标记中呈现。

Try it: https://blazorrepl.telerik.com/cQbPGRPT48cbMaCe55试试看: https://blazorrepl.telerik.com/cQbPGRPT48cbMaCe55

It is not really clear what you want to achieve.目前还不清楚你想要实现什么。 Have you looked into @ref That way you can reference you Razor child component elsewhere.您是否查看过@ref这样您就可以在其他地方引用您的 Razor 子组件。

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

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