在Blazor中,可以使用@page
指令将组件绑定到特定的路由。
下面是一个示例,展示如何将组件绑定到Blazor路由:
MyComponent.razor
的Blazor组件文件,并在文件中添加以下代码:@page "/mycomponent/{id}"
MyComponent
Id: @Id
@code {
[Parameter]
public string Id { get; set; }
}
App.razor
中使用
组件来设置路由配置,并将MyComponent
组件添加到路由配置中。在文件中添加以下代码:
Sorry, there's nothing at this address.
/mycomponent/123
,其中123
是你想要传递给MyComponent
组件的Id
值。在上面的示例中,@page
指令将MyComponent
组件绑定到/mycomponent/{id}
路由,其中{id}
是一个占位符,可以通过组件的Id
属性来获取。当导航到/mycomponent/123
时,123
将作为Id
属性的值传递给MyComponent
组件,并在页面上显示。
希望这个例子对你有帮助!
上一篇:绑定动态输入