简体   繁体   中英

angular : reload page after redirection

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']);
   }

  
}

Why 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 {
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM