在Blazor WebAssembly中,可以使用路由参数来实现同一个组件的多个路由。下面是一个示例:
App.razor
文件中定义路由配置:
Sorry, there's nothing at this address.
MyComponent.razor
的组件:My Component
Current route: @CurrentRoute
@code {
[Parameter]
public string CurrentRoute { get; set; }
}
MainLayout.razor
文件中使用MyComponent
组件,并根据需要传递不同的路由参数:
@code {
private Dictionary
routeData;
protected override void OnInitialized()
{
routeData = new Dictionary
{
{ "route1", "Route 1" },
{ "route2", "Route 2" },
{ "route3", "Route 3" }
};
}
}
NavMenu.razor
文件中创建导航菜单,并使用NavLink
组件来设置不同的路由:
这样就可以在Blazor WebAssembly中实现同一个组件的多个路由。每个路由都会渲染MyComponent
组件,并传递不同的路由参数。