简体   繁体   English

Blazor WebAssembly:同一组件渲染上的多个路由

[英]Blazor WebAssembly : Multiple route on same component rendering

I'm actually experimentating Blazor WebAssembly.我实际上是在试验 Blazor WebAssembly。

Everything works fine except one thing.除了一件事,一切都很好。

The idea is I want to share the same component for creation or edition of an item.这个想法是我想共享相同的组件来创建或编辑项目。

The name of the component is CreateOrEdit.razor and I have two routes:组件的名称是 CreateOrEdit.razor 我有两条路线:

@page "/master/maker/create"
@page "/master/maker/{id:int}"

Id is catched and stored on a property: id 被捕获并存储在一个属性中:

[Parameter]
public int Id { get; set; }

I have also a getter property to determine if we are on edit or create page.我还有一个 getter 属性来确定我们是在编辑还是创建页面。

public bool IsEditPage { get => Id > 0; }

And used into the view like this并像这样用于视图

@if (IsEditPage)
{
    <Button Clicked="OnEditButtonClicked"
            Color="@(m_EditMode ? Color.Danger : Color.Primary)" Class="mb-2">
        @(m_EditMode ? "Save" : "Edit")
    </Button>
    <Row>
        <Column ColumnSize=" ColumnSize.Is2" Class="my-auto">
            <span>ID</span>
        </Column>
        <Column ColumnSize="ColumnSize.Is8">
            <span>@Id</span>
        </Column>
        <Column ColumnSize="ColumnSize.Is2"></Column>
    </Row>
}
else
{
    <Button Clicked="OnCreateButtonClicked" Color="Color.Danger" Class="mb-2">Create</Button>
}

If we are on edition mode, we got the ID of the item and the "Edit" button displayed.如果我们处于编辑模式,我们会得到项目的 ID 并显示“编辑”按钮。 Then if we click on "Edit" button the elements on the form can be edited.然后,如果我们单击“编辑”按钮,可以编辑表单上的元素。 The "Edit" button change and become "Save". “编辑”按钮更改为“保存”。 Then data is send to the API.然后将数据发送到 API。

On creation mode, the "Save" button is displayed.在创建模式下,会显示“保存”按钮。 Just clicking on it send data to the API.只需单击它将数据发送到 API。 I plan to add validation but the problem is not here.我计划添加验证,但问题不在这里。

The problem below:下面的问题:

Assume we navigate first to the edit page "localhost/master/maker/1" and then we navigate to the creation page "localhost/master/maker/create" (problem is the same if we create an item and redirect to the edit page then);假设我们首先导航到编辑页面“localhost/master/maker/1”,然后我们导航到创建页面“localhost/master/maker/create”(如果我们创建一个项目并重定向到编辑页面,问题是一样的然后); The component is not re-rendering because it shares the same one.该组件不会重新渲染,因为它共享同一个。 Furthermore, the OnInitializedAsync() method is not once again called.此外,不会再次调用 OnInitializedAsync() 方法。 I don't want to have 2 components separated with the same code nor force loading once again because I have some scoped services on the IoC for data caching.我不想用相同的代码分隔 2 个组件,也不想再次强制加载,因为我在 IoC 上有一些用于数据缓存的范围服务。

Can someone help me?有人能帮我吗?

It's not a bug.这不是一个错误。 OnInitialized/OnInitializedAsync is called once, because the component is reused, not created again. OnInitialized/OnInitializedAsync被调用一次,因为组件被重用,而不是再次创建。 To initialize parameters you should use OnParametersSet/OnParametersSetAsync .要初始化参数,您应该使用OnParametersSet/OnParametersSetAsync These are called every time before rendering the component.每次在渲染组件之前都会调用它们。 It is described in microsoft docs [1]: https://docs.microsoft.com/en-us/aspnet/core/blazor/components/lifecycle?view=aspnetcore-3.1它在微软文档 [1] 中有描述: https://docs.microsoft.com/en-us/aspnet/core/blazor/components/lifecycle?view=aspnetcore-3.1

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

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