在Blazor中,您可以通过以下方法在布局中访问参数:
@layout MyLayout
@code {
[Parameter]
public string Title { get; set; }
}
@Title
@Body
@page "/mypage"
@layout MyLayout
@code {
protected override void OnInitialized()
{
Title = "My Page";
}
[CascadingParameter]
public string Title { get; set; }
}
在这个例子中,我们在MyPage
组件中设置了Title
参数的值为"My Page"。然后,这个值会传递给布局组件MyLayout
,并在布局中显示出来。
请注意,为了在布局中访问参数,您需要使用CascadingParameter
属性将参数声明为级联参数。这样,它才能从页面组件中传递到布局组件中。
希望这个解决方法对您有帮助!