angular:重定向后重新加载页面

时间:2020-08-21 17:32:41

标签: javascript angular typescript angular-routing angular-router

我有一个重定向到另一个页面的函数,我希望重定向后的页面也重新加载。

我要在重定向到登录页面后重新加载它。 使用“ window.location.reload()”会重新加载当前页面,该页面是主页,而不是登录页面。 如何重新加载特定页面?

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

  
}

2 个答案:

答案 0 :(得分:3)

你为什么不使用

redirection(){
   window.location.href="yourpagedomain/login"
 } 

它将加载页面。

答案 1 :(得分:1)

在登录页面上,您可以捕获导航事件,然后,如果为true,则可以重新加载表单。希望对您有所帮助:

在您的登录页面上:

OLD:代码错误

||=== Build: Release in Test (compiler: GNU GCC Compiler) ===|
Test/main.cpp|6|warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]|
Test/main.cpp||In function ‘int main()’:|
Test/main.cpp|12|error: ‘snd_pcm_t’ was not declared in this scope; did you mean ‘snd_pcm_info’?|
Test/main.cpp|12|error: ‘handle’ was not declared in this scope|
Test/main.cpp|16|error: ‘SND_PCM_STREAM_PLAYBACK’ was not declared in this scope; did you mean ‘SNDRV_PCM_STREAM_PLAYBACK’?|
Test/main.cpp|16|error: ‘snd_pcm_open’ was not declared in this scope; did you mean ‘snd_pcm_info’?|
Test/main.cpp|17|error: ‘snd_strerror’ was not declared in this scope|
||error: %s\n", snd_strerror(err));|
Test/main.cpp|21|error: ‘SND_PCM_FORMAT_U8’ was not declared in this scope; did you mean ‘SNDRV_PCM_FORMAT_U8’?|
Test/main.cpp|22|error: ‘SND_PCM_ACCESS_RW_INTERLEAVED’ was not declared in this scope; did you mean ‘SNDRV_PCM_ACCESS_RW_INTERLEAVED’?|
Test/main.cpp|20|error: ‘snd_pcm_set_params’ was not declared in this scope; did you mean ‘snd_pcm_sw_params’?|
Test/main.cpp|27|error: ‘snd_strerror’ was not declared in this scope|
||error: %s\n", snd_strerror(err));|
Test/main.cpp|31|error: ‘snd_pcm_writei’ was not declared in this scope|
Test/main.cpp|33|error: ‘snd_pcm_recover’ was not declared in this scope|
Test/main.cpp|35|error: ‘snd_strerror’ was not declared in this scope|
Test/main.cpp|42|error: ‘snd_pcm_drain’ was not declared in this scope|
Test/main.cpp|44|error: ‘snd_strerror’ was not declared in this scope|
Test/main.cpp|45|error: ‘snd_pcm_close’ was not declared in this scope|
||=== Build failed: 17 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|

编辑:此代码有效:

export class LoginPage implements OnInit {
  
  constructor(private router: Router) {
   router.events.subscribe(val => {
      if (val) {
        this.refreshForm(); // Refresh your form
      }
    });
  }
 
  ngOnInit(): void {

  }

}