在同级(组件)之间传递对象

问题描述 投票:0回答:1

说我有一个像这样的第一个组件:

<EditForm Model="@forecasts" OnValidSubmit="@HandleValidSubmit">
    <DataAnnotationsValidator />
    <ValidationSummary />
    <table class="table">
        <thead>
            <tr>
                <th>Id</th>
                <th>Date</th>
                <th>Temp. (C)</th>
                <th>Temp. (F)</th>
                <th>Summary</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var forecast in forecasts)
            {
                <tr>
                    <td><NavLink href="@($"/Edit/")">@forecast.Id</NavLink></td>
                    <td>@forecast.Date.ToShortDateString()</td>
                    <td>@forecast.TemperatureC</td>
                    <td>@forecast.TemperatureF</td>
                    <td><InputText @bind-Value="@forecast.Summary"/></td>
                </tr>
            }
                <button class="btn btn-primary" type="submit">Save</button>
        </tbody>
    </table>
</EditForm>


@code {
    List<WeatherForecast> forecasts;

    protected override async Task OnInitializedAsync()
    {
        forecasts = await Http.GetJsonAsync<List<WeatherForecast>>("https://localhost:5000/WeatherForecast/");
    }
    private async Task HandleValidSubmit()
    {
        // What should be put there???
    }
}

现在,由于我已经检索了预测对象的列表,因此我希望将其中一个传递给任何其他组件,例如,如下所示的编辑组件:

@page "/WeatherForecast/Edit/"
@inject HttpClient Http


@code {
    [Parameter]
    WeatherForecast Forecast { get; set; }


    protected override async Task OnInitializedAsync()
    {
        forecast = // **How to pass the forecast object here from the previous one** //
    }
    private async Task HandleValidSubmit()
    {
        await Http.PostJsonAsync($@"https://localhost:5000/WeatherForecast/", Forecast);
    }
}

(实际上,再次获得用于编辑的对象将是浪费和无用的……)

如何使用Blazor实现这一目标?我知道如何传递值,等等,但是不知道[[如何在同级组件之间传递自定义对象,并且到目前为止,还没有发现如何执行此操作……

blazor
1个回答
0
投票
您需要的是Routing Parameters
© www.soinside.com 2019 - 2024. All rights reserved.