【发布时间】:2019-10-30 04:19:55
【问题描述】:
我想让 if else 功能,用于活动或非活动@layout。但它不工作 @layout 始终处于活动状态。
@if (showTable == true)
{
@layout TransactionLayout
}
【问题讨论】:
标签: c# asp.net blazor blazor-server-side
我想让 if else 功能,用于活动或非活动@layout。但它不工作 @layout 始终处于活动状态。
@if (showTable == true)
{
@layout TransactionLayout
}
【问题讨论】:
标签: c# asp.net blazor blazor-server-side
根据Blazor's docs,布局在编译时被定义为一个属性:
使用 Razor 指令 @layout 将布局应用于组件。编译器将@layout 转换成一个LayoutAttribute,应用于组件类。
由于布局是作为类属性应用的,我怀疑您可以在运行时更改它。
我建议您在没有布局的情况下定义一次组件,然后在具有布局的 另一个 组件中使用它 - 并在运行时决定要渲染哪个组件。
例如:
//MyComponent.razor
<!-- your component here, without the layout -->
然后:
//MyComponentWithLayout.razor
@layout TransactionLayout
<MyComponent />
最后,在您的主要组件或页面中,您可以这样做:
@if(showTable)
{
<MyComponentWithLayout />
}
else
{
<MyComponent />
}
【讨论】:
这不是 Blazor 当前处理布局的方式...
请打开 App.razor 组件并找到 RouteView 元素。此类有一个名为 DefaultLayout 的属性,其中设置了您的应用程序的默认布局(MainLayout)。
现在,您应该从RouteView 类派生,添加两个属性来表示活动或非活动布局。做这样的事情:
public class MyRouteView : RouteView
{
// Define necessary properties
[Parameter]
public Type ActivatedLayout { get; set; }
[Parameter]
public Type DeactivatedLayout { get; set; }
// Add more code to do the work of activating and deactivating the layout
}
希望这会有所帮助...
【讨论】: