这将一个值传递到Blazor组件中
[Parameter]
public string Id { get; set; }
但是如何从URL输入参数传递值呢?
这将一个值传递到Blazor组件中
[Parameter]
public string Id { get; set; }
但是如何从URL输入参数传递值呢?
使用参数属性注解定义在组件中的公共属性,可以用于存储从父组件传递给子组件的组件参数。例如,以下代码将一个字符串参数从父组件传递给其子组件。
<ChildComponent Title="我是子组件" />
<p>@Title</p>
@code{
Public string Title { get; set; } = "Default Title";
}
或者,存储路由数据的值,例如,在url中的路由数据Start https://localhost:44396/counter/123 ,如果定义了一个名为Start的公共属性,并使用Parameter属性进行注释,则框架会自动完成:
@page "/counter"
@page "/counter/{start:int}"
<h1>Counter</h1>
<p>Current count: @Start</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
[Parameter]
public int Start { get; set; }
private void IncrementCount()
{
Start++;
}
}
注意,计数器组件的定义包含两个路由模板。第一个路由模板设置为使用Start属性的默认值;即值为0。但用户可以通过在url中提供路由参数来更改默认值,例如:https://localhost:44396/counter/123。现在当按钮被点击时,起始值为123,而不是0。仅供参考,第二个模板包含一个路由约束,该约束将应用程序限制为仅使用int值。
相关内容在文档中: 组件路由
@page
指令开头的 Blazor 组件。.razor
扩展名的 Razor 组件。OnParameterSet()
或 OnParameterSetAsync()
方法。
@page "/path/to/page/{id:string}"
? - itminus