« Blazor component » : différence entre les versions
De Banane Atomic
Aller à la navigationAller à la recherche
Ligne 50 : | Ligne 50 : | ||
} | } | ||
} | |||
</filebox> | |||
= [https://docs.microsoft.com/en-us/aspnet/core/blazor/components/data-binding?view=aspnetcore-5.0#binding-with-component-parameters Binding with component parameters] = | |||
<filebox fn='Components/MyComponent.razor.cs'> | |||
[Parameter] | |||
public string Description { get; set; } | |||
@* name of the EventCallback has to be [PropertyName]Changed *@ | |||
[Parameter] | |||
public EventCallback<string> DescriptionChanged { get; set; } | |||
private async Task UpdateDescription() | |||
{ | |||
await DescriptionChanged.InvokeAsync(description); | |||
} | |||
</filebox> | |||
<filebox fn='Pages/Index.razor'> | |||
<MyComponent @bind-Description="description" /> | |||
@code { | |||
private string description = "Text"; | |||
} | } | ||
</filebox> | </filebox> |
Version du 25 juillet 2021 à 15:15
Basic component
Components/MyComponent.razor |
<h1>Test</h1> <p>@ChildContent</p> |
Components/MyComponent.razor.cs |
public partial class MyComponent : ComponentBase { [Parameter] public RenderFragment ChildContent { get; set; } } |
Components/MyComponent.razor.css |
h1 { color: coral; } |
Pages/Index.razor |
@page "/" @using Component.Components <MyComponent> test !!! </MyComponent> |
Callback event
Components/MyComponent.razor.cs |
[Parameter] public EventCallback<string> Callback { get; set; } private string description; private async Task Ok() { await Callback.InvokeAsync(description); } |
Pages/Index.razor |
<MyComponent Callback="MyComponentCallback" /> @code { void MyComponentCallback(string description) { } } |
Binding with component parameters
Components/MyComponent.razor.cs |
[Parameter] public string Description { get; set; } @* name of the EventCallback has to be [PropertyName]Changed *@ [Parameter] public EventCallback<string> DescriptionChanged { get; set; } private async Task UpdateDescription() { await DescriptionChanged.InvokeAsync(description); } |
Pages/Index.razor |
<MyComponent @bind-Description="description" /> @code { private string description = "Text"; } |