我试图跟进“捕获对元素的引用”以将更改从子组件传递给父组件,但它不起作用 url 是https://docs.microsoft.com/en-us/aspnet/core/blazor/javascript-interop ?view=aspnetcore-3.0#capture-references-to-elements
请问可以帮我吗?a) 代码有什么问题吗?b)我可以在哪里找到“Console.WriteLine”信息,因为我无法从 vs 输出窗口找到它。
1. ParentComponent.razor
@page "/parentcomponent"
@using BlazorApp.Components
<h1>Parent Component</h1>
<p>ParentYear: @ParentYear</p>
<ChildComponent @ref="ChildComponent1" @bind-Year="ParentYear" />
@code {
private int ParentYear = 1978;
ChildComponent ChildComponent1 = new ChildComponent();
protected override void OnInitialized()
{
ChildComponent1.YearChanged += ChildFiredEvent;
}
public void ChildFiredEvent(int _year)
{
ParentYear = _year;
Console.WriteLine(ParentYear.ToString());
StateHasChanged();
}
}
2. Childcomponent.razor
<h2>Child Component</h2>
<p>ChildYear: @Year</p>
<button class="btn btn-primary" @onclick="ChangeTheYear">Change Year to 1986</button>
@code {
[Parameter]
public int Year { get; set; }
[Parameter]
public Action<int> YearChanged { get; set; }
private void ChangeTheYear()
{
Year = 1986;
YearChanged?.Invoke(Year);
}
}