这会将值传递给 blazor 组件
[Parameter]
public string Id { get; set; }
但是如果从 URL 输入参数传递一个值呢?
在组件内定义并用 Parameter 属性注释的公共属性用于存储从其父组件传递到子组件的 Component 参数,例如,以下代码将字符串参数从父组件传递到其子组件子组件。
父组件.razor
<ChildComponent Title="I'm a child component" />
ChildComponent.razor
<p>@Title</p>
@code{
Public string Title { get; set; } = "Default Title";
}
或者,存储路线数据的值,例如,路线数据Start在url中https://localhost:44396/counter/123 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++;
}
}
请注意,Counter 组件的定义包含两个路由模板。第一个路由模板设置为使用 Start 属性的默认值;即值 0。但是用户可以通过在 url 中提供路由参数来更改默认值,例如:https://localhost:44396/counter/123 https://localhost:44396/counter/123。现在,当单击该按钮时,起始值为 123,而不是 0。仅作为记录,第二个模板包含一个路由约束,该约束限制应用程序仅使用 int 值。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)