I have this component with some parameters that take a value within the same component. What I want is to call the component from another place and there obtain the value of those parameters (not change or assign them, but capture them in new variables) to perform some validations with those values. This is so that a component is initialized by validating in the local storage if there is any key UserMail and if so it captures the value and the idea is to validate and use the component on other pages where at the beginning it is checked if the component contains value in userCorreo and if so, render the pages with that layout My component with parameters:
@inject NavigationManager NavigationManager
@inject Blazored.LocalStorage.ILocalStorageService _InjectILocalStorage
@rendermode InteractiveServer
<p>@_UserCorreo</p>
@code {
public string _UserCorreo { get; set; } = "A";
public bool _ValidateUserCorre { get; set; } = false;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
_UserCorreo = await _InjectILocalStorage.GetItemAsync<string>("UserCorreo");
if (string.IsNullOrEmpty(_UserCorreo))
{
NavigationManager.NavigateTo("/Login", forceLoad: true);
}
else
{
StateHasChanged();
}
}
}
}
Where I want to call it (in the mainLayout):
@inherits LayoutComponentBase
<!-- Usando la referencia del componente UserName -->
<UserName @ref="Us" />
<!-- Accediendo a las propiedades públicas del componente UserName -->
<p>@Us._UserCorreo</p>
@if (Us._ValidateUserCorre)
{
<div class="page">
<div class="sidebar bg-1">
<NavMenu />
</div>
<main>
<div class="top-row px-4 bg-2">
<span class="mx-3 text-secondary">Hi, @Us._UserCorreo</span>
<div class="contenedor-img-perfil bg-light"></div>
</div>
<article class="content px-4">
@Body
</article>
</main>
</div>
<div id="blazor-error-ui">
An unhandled error has occurred.
<a href="" class="reload">Reload</a>
<a class="dismiss">????</a>
</div>
}
@code {
// Declaración del componente UserName
private UserName Us;
// Método OnInitialized para inicializar el componente
protected override void OnInitialized()
{
// Inicialización del componente UserName
}
}
What I want is to do this way, have a component that calls the new pages and this has the information of whether there is an email in the local storage with a specific key, if there is, render the page or if not, redirect from the component to login…what I need is from a component to extract the values of the component that already detects whether the email exists or not and even redirects to login if it does not, and if it exists, it assigns it to a variable.. I’m Open to new methods, if possible simple, quality and consistent methodologies.
Ferdy Andrés is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1