在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组件,并在页面上显示。
希望这个例子对你有帮助!
上一篇:绑定动态输入