-
Notifications
You must be signed in to change notification settings - Fork 22
Blazor 创建自定义 Blazor 组件
L edited this page Mar 20, 2024
·
2 revisions
创建组件文件CustomComponent.razor
编写组件的 HTML 结构:在 .razor 文件中,我们可以编写组件的 HTML 结构。这可以包括标准的 HTML 元素,也可以包括其他 Blazor 组件。
<h1>@Title</h1>
<p>@Description</p>
添加组件的 C# 代码:在 .razor 文件中,我们可以添加 C# 代码来定义组件的属性、字段、方法等。
@code {
[Parameter]
public string Title { get; set; }
[Parameter]
public string Description { get; set; }
}
在上面的代码中,我们定义了两个参数 Title 和 Description,它们可以在使用组件时被设置。
使用组件 创建完组件后,我们就可以在其他 .razor 文件中使用它了。只需像使用 HTML 元素一样使用组件即可。
<CustomComponent Title="Hello, World!" Description="This is a custom Blazor component." />
注意,组件的标签名通常与其文件名(不包括 .razor 扩展名)相同。