在Blazor中,可以通过以下方式将参数传递给自定义布局:
@layout MyLayout
My Content
@code {
private string myParameter = "Hello";
protected override void OnInitialized()
{
base.OnInitialized();
// 可以在这里根据需要设置参数的值
}
}
@inherits LayoutComponentBase
My Layout
Parameter: @MyParameter
@Body
@code {
[Parameter]
public string MyParameter { get; set; }
}
public static async Task Main(string[] args)
{
// ...
builder.Services.AddScoped();
// ...
}
@inherits LayoutComponentBase
My Layout
Parameter: @MyParameter
@Body
@code {
[Parameter]
public string MyParameter { get; set; }
}
通过上述步骤,你可以实现将参数传递给自定义布局并在布局组件中使用它。确保在需要传递参数的Razor页面中设置正确的参数值。