Angular: refresh page post redirection

I am working on a function that redirects to another page, and I am looking to have the redirected page reload once the redirection is complete.

I specifically need the login page to reload after being redirected to it. The usual 'window.location.reload()' only reloads the current page, which is the home page, not the login page. How can I reload a specific 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']);
   }
  
}

Answer №1

Have you considered implementing

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

This method ensures the page is properly loaded.

Answer №2

When accessing your login page, you have the opportunity to capture the navigate event. If this event is true, you can proceed to reload the form. Below is a revised version of the code to assist you:

On your login page:

OLD : Incorrect code

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

  }

}

EDIT : Updated and functional code :

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the preferred method for accessing nested object properties in React props?

Building upon a previous inquiry - Javascript - How do I access properties of objects nested within other Objects It appears that standard dot notation doesn't suffice for accessing nested object properties within React state/props. In the case of t ...

Analyzing the Threejs application for profiling purposes

Recently, I noticed that my webgl application developed using threejs is facing some performance issues in terms of FPS on certain machines. To diagnose the problem, I attempted profiling my application using Chrome's about:tracing feature, following ...

Introducing the First Angular Factory

It's high time for me to finally inject my first angular Factory . . . Here is the code I have: .factory('Debts', function($q, $scope){ return MA; }) .controller('Admin', function ($scope, Debts) { $scope.Debts = Deb ...

Retrieve the chosen date from a DatePicker using a Javascript function

Is there a way to retrieve the user-selected date using a JS function? I attempted this method: var test = $("#datepicker").datepicker( 'getDate' ); However, when I display it with an alert, it shows [object object] instead of the date. This ...

Issue with ThreeJs: Difficulty loading separate images on top and bottom surfaces

I've been trying to place different textures on the top and bottom sides using Threejs to display unique images on each side. However, I'm encountering an issue where the same image is being displayed on both the top and bottom sides. Below is th ...

Retrieving data from an array using an AJAX request function

I've been attempting to utilize an AJAX call to update a series of image elements on a webpage. The array containing the elements to update is populated with URLs fetched from a PHP page via AJAX. The issue I'm encountering with the code provide ...

Using framer-motion with Next.JS ensures that content remains consistent during navigation

I added a Link on my homepage that connects to the About Us page: <Link href="/about"><a>About us</a></Link> In my _app.js file, there is an AnimatePresence wrapper: <AnimatePresence exitBeforeEnter> <Component {...p ...

Disable other actions during jquery change event validation

Is there an answer for this question already? If so, I apologize! Here is my situation: I have an edit field that is validated when the .change() event occurs. What I am trying to achieve is this: if a user types something and then immediately clicks the s ...

Finding out which side of a cube has been clicked---Let's identify the side

Currently, I am in the process of developing a navigation menu and I am looking for a way to detect which side the user has clicked. Is it possible to achieve this using raycasting or is there an alternative method that would work better? If you require i ...

When attempting to send data using jQuery to PHP, an issue arises where PHP is unable to receive the information, resulting in an undefined variable

Has anyone successfully sent data from an HTML select to a PHP file using jQuery? I've tried multiple solutions suggested here, but none seem to be working for me. Below is the code I'm using: <select id="city" name="city" > <optgro ...

Disappear solely upon clicking on the menu

Currently, I am working on implementing navigation for menu items. The functionality I want to achieve is that when a user hovers over a menu item, it extends, and when they move the mouse away, it retracts. I have been able to make the menu stay in the ex ...

Tips for displaying autocomplete suggestions as clickable links?

I am new to using Ajax and trying to figure out how to display autocomplete results as clickable links. I have managed to list the related results, but I am struggling to add the links. I believe the links need to be added within the script as an HTML tag. ...

Site performance stalls when a trailing slash is present in the URL while using Express routing

Whenever I visit a page on my development site with a URL like localhost:8000/news, everything loads perfectly fine. However, if I add a trailing slash to the URL, such as localhost:8000/news/, the entire site freezes. It seems to be trying to load partial ...

IE throws an exception when attempting to use Canvas as it is not supported

One of my challenges involves working with a Canvas Element: <canvas id="canvas" width="300" height="300"> Sorry, your browser does not support the Canvas element </canvas> In my JavaScript file, I have the following code: var ct= docum ...

Changing a table cell's value when a button is clicked using Angular

I'm working with a bootstrap table that includes a button and a textbox above it. After entering a number in the textbox and clicking the generate button, I need to update the "Bill Amount" column for each row with the value from the textbox. How can ...

Using dynamic tag names within React JSX can greatly enhance the flexibility and

I'm working on creating a React component for HTML heading tags (h1, h2, h3, etc.), where the heading level is determined by a prop. Here is how I attempted to approach it: <h{this.props.level}>Hello</h{this.props.level}> My expected out ...

Creating a tool to display mouse coordinates as a pointer on an HTML5 canvas

I'm struggling with mouse coordinates while working on canvas in HTML5. My approach involves using JavaScript for drawing on the canvas. Below is my current code to track the mouse pointer within the canvas: $('#canvasID').mousedown(functi ...

What could be the reason for TypeScript throwing an error that 'product' does not exist in type '{...}' even though 'product' is present?

Structure of Prisma Models: model Product { id String @id @default(auto()) @map("_id") @db.ObjectId name String description String price Float image String createdAt DateTime @default(now()) updatedAt Da ...

Experiencing a problem with XAMPP? Changes made to the code are not reflected after saving

Recently, I've encountered a strange issue with my XAMPP test server while working on a game project. Everything was running smoothly until I noticed that when I make changes to certain files in Notepad++ and save them, the updates are not being refle ...

What should I do about typescript and ES6?

An error occurred while running my code: [0] app/components/people/details/PersonDetailComponent.ts(27,35): error TS2339: Property 'person' is missing from type '{}'. Here is the code snippet in question: export class PersonDeta ...