因此,我想在子组件(TimeEntryForm)中调用OpenModal()方法。我认为我需要访问TimeEntryForm组件的实例,但是我不知道如何创建TimeEntryForm的命名实例。
任何帮助将不胜感激。
父组件
<button type="button" onclick="@TimeEntryForm.OpenModal()" class="btn btn-info btn-lg">
<span class="glyphicon glyphicon-plus"></span> Add
</>
<TimeEntryForm></TimeEntryForm>
TimeEntry子组件
@using Chopper.Shared
@using DatabaseModel.Models
@page "/timeentryform"
@inject HttpClient HTTP
@inherits TimeEntryFormModel
@if (IsOpened)
{
<div class="modal" role="dialog" tabindex="-1" style="display: block;">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Add a Time Entry</h5>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label for="Job" class="col-form-label">Job:</label>
<input type="text" class="form-control" />
</div>
<div class="form-group">
<label for="Employee" class="col-form-label">Employee:</label>
<input class="form-control" type="text" />
</div>
<div class="form-group">
<label for="Hours" class="col-form-label">Hours:</label>
<input class="form-control" type="text" />
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" onclickAsync="@OnAddTimeEntry()">Add Entry</button>
<button type="button" class="btn btn-primary" onclick="@CloseModal">Close</button>
</div>
</div>
</div>
</div>
<div class="modal-backdrop show"></div>
}
视图模型后面的时间输入子组件代码
using Chopper.Client.TimeEntries.Services;
using DatabaseModel.Models;
using Microsoft.AspNetCore.Blazor.Components;
using System.Threading.Tasks;
namespace Chopper.Client.TimeEntries
{
public class TimeEntryFormModel : BlazorComponent
{
protected TimeEntry TimeEntry = new TimeEntry();
[Inject]
private TimeEntriesServices _client { get; set; }
protected bool IsOpened { get; set; }
public void OpenModal()
{
IsOpened = true;
StateHasChanged();
}
//[Parameter]
protected void CloseModal()
{
IsOpened = false;
StateHasChanged();
}
protected async Task OnAddTimeEntry()
{
if (TimeEntry.Job != null && TimeEntry.Category != null && TimeEntry.TimeSpentHours > 0 && TimeEntry.Employee != null)
{
await _client.CreateTimeEntry(TimeEntry);
await OnInitAsync();
StateHasChanged();
//return true;
}
//return false;
}
}
}
答案 0 :(得分:1)
所以我需要在protected TimeEntryForm timeEntryForm = new TimeEntryForm();
的父组件模型中添加一个受保护的成员
,然后在视图中将其引用为<TimeEntryForm ref="timeEntryForm"></TimeEntryForm>
父组件
<button type="button" onclick="@timeEntryForm.OpenModal()" class="btn btn-info btn-lg">
<span class="glyphicon glyphicon-plus"></span> Add
</>
<TimeEntryForm ref="timeEntryForm"></TimeEntryForm>
模型/视图模型后面的父组件代码
public class TimeEntriesGridModel : BlazorComponent
{
[Inject]
private TimeEntriesServices _client { get; set; }
protected TimeEntryForm timeEntryForm; // = new TimeEntryForm(); //Per Kirk Woll's suggestion
protected List<TimeEntry> Model { get; set; }
protected override async Task OnInitAsync()
{
Model = await _client.GetAllTimeEntries();
}
}
TimeEntry子组件
@using Chopper.Shared
@using DatabaseModel.Models
@page "/timeentryform"
@inject HttpClient HTTP
@inherits TimeEntryFormModel
@if (IsOpened)
{
<div class="modal" role="dialog" tabindex="-1" style="display: block;">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Add a Time Entry</h5>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label for="Job" class="col-form-label">Job:</label>
<input type="text" class="form-control" />
</div>
<div class="form-group">
<label for="Employee" class="col-form-label">Employee:</label>
<input class="form-control" type="text" />
</div>
<div class="form-group">
<label for="Hours" class="col-form-label">Hours:</label>
<input class="form-control" type="text" />
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" onclickAsync="@OnAddTimeEntry()">Add Entry</button>
<button type="button" class="btn btn-primary" onclick="@CloseModal">Close</button>
</div>
</div>
</div>
</div>
<div class="modal-backdrop show"></div>
}
视图模型后面的时间输入子组件代码
using Chopper.Client.TimeEntries.Services;
using DatabaseModel.Models;
using Microsoft.AspNetCore.Blazor.Components;
using System.Threading.Tasks;
namespace Chopper.Client.TimeEntries
{
public class TimeEntryFormModel : BlazorComponent
{
protected TimeEntry TimeEntry = new TimeEntry();
[Inject]
private TimeEntriesServices _client { get; set; }
protected bool IsOpened { get; set; }
public void OpenModal()
{
IsOpened = true;
StateHasChanged();
}
//[Parameter]
protected void CloseModal()
{
IsOpened = false;
StateHasChanged();
}
protected async Task OnAddTimeEntry()
{
if (TimeEntry.Job != null && TimeEntry.Category != null && TimeEntry.TimeSpentHours > 0 && TimeEntry.Employee != null)
{
await _client.CreateTimeEntry(TimeEntry);
await OnInitAsync();
StateHasChanged();
//return true;
}
//return false;
}
}
}
EIA:
根据Kirk Woll的建议删除了timeEntryForm初始化后的代码。
答案 1 :(得分:0)
同志,
请,请按照下列步骤操作:
1.在父组件中定义一个事件委托,该委托的对象是告诉孩子
组件以打开模式窗口:
public event Action OnOpenModal;
2.在父组件中定义一个调用事件委托的方法
private void OpenMe() => OnOpenModal?.Invoke();
3.当用户单击父组件中定义的按钮时,将调用此方法:
注意:不要在OpenMe之后使用括号
<button type="button" onclick="@OpenMe" class="btn btn-info btn-lg">
<span class="glyphicon glyphicon-plus"></span> Add
</>
4.使用事件委托的值向子组件元素添加属性MyModal。
<TimeEntryForm MyModal = "@OnOpenModal"></TimeEntryForm>
Define a property of Action delegate to store the event delegate passed to the Child Component
[Parameter] protected Action MyModal { get; set; }
protected override void OnInit()
{
// Attach the OpenModal method defined in the Child // Component to the delegate
MyModal += OpenModal;
}
Now, whenever the user hit the button the OpenModal is called and the modal window
is displayed.
public void OpenModal()
{
IsOpened = true;
StateHasChanged();
}