基本组件布局继承类 [英] basic component layout inheritance blazor

查看:51
本文介绍了基本组件布局继承类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我的大多数组件都有一个标头.我想创建一个具有header变量的基本组件,并使所有其他组件从该组件继承并设置header.所以我有

Let's say most of my components have a header. I want to create a base component that has the header variable and make all the other components inherit from that component and set the header. So I have

BaseComponent

@inherits LayoutComponentBase;

<h1>@header</h1>

@Body

@code {

    protected string header;
}

组件

@inherits BaseComponent;

"internal component text"

@code {
  protected override void OnInitialized()
    {
         base.header = "Setting header for the parent"
    }
}

这将编译并显示没有错误,但基本头没有显示.就像基础中的任何东西都没有被渲染一样.我在做什么错了?

This compiles and shows up with no errors but the base header doesn't show up. It's like anything in the base doesn't get rendered. What am I doing wrong?

P.S.

我还尝试了 @layout BaseComponent ,甚至同时尝试了这两个指令.

I have also tried @layout BaseComponent, and even both directives at the same time.

推荐答案

在撰写本文时,派生的razor组件自动实现其基类的所有方法,包括BuildRenderTree(呈现您在razor文件中键入的HTML标记)..当您不键入任何内容时,该方法将不会尝试自行调用基本BuildRenderTree方法.因此,您需要像这样手动进行操作:

As of this writing, derived razor components implement all methods of their base classes automatically, including BuildRenderTree (which renders the HTML markup that you type within the razor file). When you type nothing, that method will make no attempt at calling the base BuildRenderTree method on its own. So you need to do it manually like so:

@inherits BaseComponent;

@{
    base.BuildRenderTree(__builder);
}

@code {
  protected override void OnInitialized()
    {
         base.header = "Setting header for the parent"
    }
}

这篇关于基本组件布局继承类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆