Ale*_*xey 0 blazor blazor-server-side
我有Foo.razor:
@foreach (var item in items) {
<p>@item</p>
}
@code {
private List<string> items = new List<string>();
public void Append(string item) => items.Add(item);
}
Run Code Online (Sandbox Code Playgroud)
如果我有Bar.razor:
<Foo/>
@code {
public void SomeMethod() {
...
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能Append("something")从打电话SomeMethod()?
下面是使用属性的示例@ref。@ref提供一种引用组件实例的方法。请参考:Blazor组件
@foreach (var item in items)
{
<p>@item</p>
}
@code {
private List<string> items = new List<string>();
public void Append(string item) =>items.Add(item);
public void Refresh()
{
this.StateHasChanged(); //To refresh component after we have added items
}
}
Run Code Online (Sandbox Code Playgroud)
<Foo @ref="foo"/>
<button class="btn btn-primary" @onclick="SomeMethod">Add items</button>
@code {
Foo foo;
public void SomeMethod()
{
foo.Append("item1");
foo.Refresh();
}
}
Run Code Online (Sandbox Code Playgroud)
笔记:
The component variable is only populated after the component is rendered and its output includes the component's element. Until that point, there's nothing to reference. To manipulate components references after the component has finished rendering, use the OnAfterRenderAsync or OnAfterRender methods.
| 归档时间: |
|
| 查看次数: |
3852 次 |
| 最近记录: |