【发布时间】:2021-12-03 16:04:15
【问题描述】:
有没有办法强制 Blazor 布局在其组件之前初始化?我发现它并不总是一致的,具体取决于访问页面的方式/时间。当我在组件所依赖的 Layout 中定义 CascadingParameter 时,它会导致问题。
组件设置:
@page "/parent1/parent2/parent3/things/1/details"
@layout MyLayout
// show thing here
@code
{
[CascadingParameter] public object Thing { get; set; } // can be null
protected override void OnInitialized()
{
// this may occur before or after the layout is initialized
// do something with Thing
}
}
布局设置:
@inherits LayoutBase
@if (thing is not null) {
<CascadingValue Value="@thing" IsFixed="true">
@Body
</CascadingValue>
}
@code {
object thing;
protected override void OnInitialized()
{
// this may occur before or after the component is initialized
thing = GetThingFromDatabase();
// validate thing here
}
}
我认为布局将是保护/验证/授权路由参数然后为任何子页面存储对象的最佳位置,但如果时间是随机的,则不起作用。例如,我追求的页面结构如下。
/things/1 (layout/abstract page)
/things/1/details (page using layout)
/things/1/otherStuff (page using layout)
/things/1/moreStuff (page using layout)
我只想加载一次,并在子页面之间导航时保留它。
【问题讨论】:
-
App.razor将您的路由器设置为使用 MainLayout。更具体地说是RouteView。