I have a function which redirects to another page, I want that after redirection the redirected page also reloads.
I want to reload the login page after being redirected to it. using 'window.location.reload()' reloads the current page which is home page but not the login page. How to reload a specefic page?
homeComponent.ts
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormGroup, FormBuilder, FormControl, Validators, Form } from '@angular/forms';
import { LoginComponent } from '../authentification/login/loginponent';
@Component({
selector: 'app-home',
templateUrl: './homeponent.html',
styleUrls: ['./homeponent.scss']
})
export class HomeComponent implements OnInit {
ngOnInit(): void {
}
constructor(private router: Router) { }
redirection(){
this.router.navigate(['/login']);
}
}
I have a function which redirects to another page, I want that after redirection the redirected page also reloads.
I want to reload the login page after being redirected to it. using 'window.location.reload()' reloads the current page which is home page but not the login page. How to reload a specefic page?
homeComponent.ts
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormGroup, FormBuilder, FormControl, Validators, Form } from '@angular/forms';
import { LoginComponent } from '../authentification/login/login.component';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
ngOnInit(): void {
}
constructor(private router: Router) { }
redirection(){
this.router.navigate(['/login']);
}
}
Share
Improve this question
edited Aug 21, 2020 at 18:18
firas1
asked Aug 21, 2020 at 17:32
firas1firas1
1691 gold badge3 silver badges14 bronze badges
2 Answers
Reset to default 15Why don't u use
redirection(){
window.location.href="yourpagedomain/login"
}
it will load page .
On your login page you can catch the navigate event, and then if it is true, you can reload you're form. I hope it will help you :
On your login page:
OLD : Wrong code
export class LoginPage implements OnInit {
constructor(private router: Router) {
router.events.subscribe(val => {
if (val) {
this.refreshForm(); // Refresh your form
}
});
}
ngOnInit(): void {
}
}
EDIT : this code works :
export class LoginPage implements OnInit {
constructor(private router: Router) {
router.events.pipe(
filter((events: RouterEvent) => events instanceof NavigationEnd),
).subscribe((val) => {
if (val.url === 'LoginPage Url') { // Fill with your loginPage Url (eg. /tabs/tab1)
this.refresh(); // Refresh your form
}
});
}
ngOnInit(): void {
}
}