最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - angular : reload page after redirection - Stack Overflow

programmeradmin0浏览0评论

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
Add a comment  | 

2 Answers 2

Reset to default 15

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 {
  }
}
发布评论

评论列表(0)

  1. 暂无评论