简体   繁体   English

If else function for active or deactive @layout blazor

[英]If else function for active or deactive @layout blazor

i want to make if else function, for active or deactive @layout.我想为活动或非活动@layout 制作其他 function。 but its not working the @layout always actived.但它不工作 @layout 始终处于活动状态。

@if (showTable == true)
{
    @layout TransactionLayout
}

According to Blazor's docs , the layout is defined as an attribute at compile time :根据Blazor 的文档,布局在编译时被定义为一个属性

Use the Razor directive @layout to apply a layout to a component.使用 Razor 指令 @layout 将布局应用于组件。 The compiler converts @layout into a LayoutAttribute, which is applied to the component class.编译器将@layout 转换为一个LayoutAttribute,该属性应用于组件class。

Since the layout is applied as a class attribute, I doubt you can change it at runtime.由于布局被应用为 class 属性,我怀疑你可以在运行时更改它。

I would suggest you define your component once without the layout, then use it in another component with the layout - and decide at runtime which component to render.我建议您在没有布局的情况下定义一次组件,然后在另一个具有布局的组件中使用它 - 并在运行时决定要渲染哪个组件。

For example:例如:

//MyComponent.razor
<!-- your component here, without the layout -->

then:然后:

//MyComponentWithLayout.razor
@layout TransactionLayout

<MyComponent />

and finally, in your main component or page, you can do:最后,在您的主要组件或页面中,您可以执行以下操作:

@if(showTable)
{
    <MyComponentWithLayout />
}
else
{
    <MyComponent />
}

This is not the way how Blazor handle layouts currently...这不是 Blazor 当前处理布局的方式......

Please, open the App.razor component and locate the RouteView element.请打开 App.razor 组件并找到 RouteView 元素。 This class has an attribute property named DefaultLayout where the default layout of your app (MainLayout) is set.这个 class 有一个名为 DefaultLayout 的属性,其中设置了应用程序的默认布局 (MainLayout)。

Now, you should derive from the RouteView class, add two properties to represent the active or deactive layouts.现在,您应该从RouteView class 派生,添加两个属性来表示活动或非活动布局。 Do something like this:做这样的事情:

MyRouteView.cs MyRouteView.cs

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
}

Hope this helps...希望这可以帮助...

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

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