所以我想在子组件(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;
}
}
}