对于此问题,我们需要确保将组件的基类设为 ComponentBase
,并为它添加必要的指令。下面是一个示例:
@using Microsoft.AspNetCore.Components
@Title
@code {
[Parameter]
public string Title { get; set; }
protected override void OnInitialized()
{
Title = "My Component Title";
}
}
在这个示例中,我们定义了一个新组件并将其基类设置为 ComponentBase
。我们还为组件添加了必要的 [Parameter]
指令,使得视图可以正确地渲染。在 OnInitialized
方法中,我们将 Title
设置为 My Component Title
。
这样做后,我们就可以在其他视图中使用此组件,而不需要将其视图继承自共享视图。
@page "/my-page"
@code {
// ...
}