Hola, descubrimos la solución a tu pregunta, deslízate y la encontrarás más abajo.
Ejemplo 1: Cómo recargar un componente en Angular
reloadComponent()let currentUrl =this.router.url;this.router.routeReuseStrategy.shouldReuseRoute=()=>false;this.router.onSameUrlNavigation ='reload';this.router.navigate([currentUrl]);
Ejemplo 2: página de actualización angular sin recargar
this.router.navigate(['path/to']).then(()=>
window.location.reload(););
Ejemplo 3: página actual de recarga angular 6
importDOCUMENTfrom'@angular/common';import Component, Inject from'@angular/core';
@Component(
selector:'app-refresh-banner-notification',
templateUrl:'./refresh-banner-notification.component.html',
styleUrls:['./refresh-banner-notification.component.scss'])exportclassRefreshBannerNotificationComponentconstructor(
@Inject(DOCUMENT)private _document: Document
)refreshPage()this._document.defaultView.location.reload();
Ejemplo 4: componente de actualización angular sin recargar la página
/*You can use a BehaviorSubject for communicating between different
components throughout the app.
You can define a data sharing service containing the BehaviorSubject to
which you can subscribe and emit changes.
Define a data sharing service
*/import Injectable from'@angular/core';import BehaviorSubject from'rxjs';
@Injectable()exportclassDataSharingServicepublic isUserLoggedIn: BehaviorSubject<boolean>=newBehaviorSubject<boolean>(false);/*Add the DataSharingService in your AppModule providers entry.
Next, import the DataSharingService in your and in the
component where you perform the sign-in operation. In
subscribe to the changes to isUserLoggedIn subject:
*/ import DataSharingService from'./data-sharing.service';exportclassAppHeaderComponent// Define a variable to use for showing/hiding the Login button
isUserLoggedIn:boolean;constructor(private dataSharingService: DataSharingService)// Subscribe here, this will automatically update // "isUserLoggedIn" whenever a change to the subject is made.this.dataSharingService.isUserLoggedIn.subscribe( value =>this.isUserLoggedIn = value;);/*In your html template, you need to add the *ngIf
condition e.g.:
*/ <button *ngIf="!isUserLoggedIn">Login</button><button *ngIf="isUserLoggedIn">Sign Out</button>// Finally, you just need to emit the event once the user has logged in e.g:someMethodThatPerformsUserLogin()// Some code // .....// After the user has logged in, emit the behavior subject changes.this.dataSharingService.isUserLoggedIn.next(true);
Si te ha sido de utilidad nuestro post, sería de mucha ayuda si lo compartes con el resto desarrolladores así nos ayudas a difundir este contenido.
¡Haz clic para puntuar esta entrada!
(Votos: 2 Promedio: 4.5)