After pressing the login button, my intention is to transition to a different page

I am relatively new to web development and working with angular. I have a login component that, upon hitting the LOGIN button, should redirect to another component on a different page. However, currently, when I click the button, it loads the data of the other component on the same page while still displaying the previous login component. I actually want the new component to be displayed on a separate page.

Below is the code structure:

<body>
    <app-loginpage>
        <div id="loader"><img src="assets/img/loaderpreview.svg"></div>
    </app-loginpage>

</body>

In the loginpage.component.html file:

<div class="text-center">
          <button (click)="redirect()" class="btn btn-primary btn-lg loginbtn">
           Login
          </button>
<router-outlet *ngIf="loadcomponent"></router-outlet>

In the loginpage.component.ts file:

export class LoginpageComponent implements OnInit {
  private loadcomponent = false;
  name: string;
  password: any;
  constructor(private router: Router) { }
  ngOnInit() {
  }
redirect() {
      this.loadcomponent = true;
  }

The app.routing.ts file contains the routing setup for the application.

I expect the AdminLayoutComponent to be loaded on a separate page after clicking the login button, but currently, it's loading on the same page. The provided screenshots illustrate the current behavior:

Screenshot showing the Login Component

Screenshot after clicking the login button, indicating the undesired behavior where both components are displayed on the same page

Apologies in advance if I've overlooked something obvious or made a silly mistake.

Answer №1

One way to handle rendering the <app-loginpage> only when a user is not logged in is by placing the <router-outlet> on the main app template, typically found in the app.component.html. Here's an example:

index.html

<body>
  <app-root></app-root>
</body>

app.component.html

<app-loginpage *ngIf="!authenticated"></app-loginpage>
<router-outlet *ngIf="authenticated"></router-outlet>

Answer №2

I recommend separating the login and admin layouts, both accessed through the router.

index.html

<body>
    <my-app></my-app>
</body>

my-app.component.html

<router-outlet></router-outlet>

app.routing.ts

// ......
const routes: Routes  = [
    {
        path: 'login',
        component: LoginPageComponent
    },
    {
        path: 'admin',
        canActivate: [AuthGuard],
        component: AdminLayoutComponent
    },
]
// ......

Next, address these questions:

  1. How can you determine if a user is already logged in?
  2. How can you prevent access to the Admin layout when a user is logged out? Use Angular router guards (see https://angular.io/guide/router#milestone-5-route-guards).

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

Implement a vertical background sprite animation using jQuery along with a mask

Is it possible to create a jQuery background vertical animation that smoothly transitions the sprite position, giving the appearance of fading into the next position? Currently, my code (view demo jsfiddle link below) moves the sprite to the correct backgr ...

What is the best way to pass a generic interface to the zustand create function in a TypeScript environment

Having trouble figuring out the right syntax to pass a generic interface when calling a function that accepts a generic type. My goal is to use: const data = itemStore<T>(state => state.data) import { create } from "zustand"; interface ...

Error message: In Angular 8 using Jest instead of Jasmine, there is no exported member called 'SpyObj'

I recently made a switch in my Angular CLI project from Karma to Jest by following this helpful tutorial. However, after the transition, I've noticed that some tests are passing while others are failing. One issue I encountered involves declaring a v ...

A fixed HTML div with a relative div is like having your own

I am in search of two divs with the following styles: <div style="height:20px;" /> <div style="height:100%;" /> These divs result in one being 20px tall and the other spanning 100% of the screen height, causing a vertical scrollbar that is eq ...

Browsing through dual snap points simultaneously on Google Chrome

I'm currently developing a website that incorporates scroll snapping for 3 viewport-sized <section> elements set up like so: html, body { scroll-behavior: smooth; scroll-snap-type: mandatory; scroll-snap-points-y: repeat(100vh); scrol ...

Use jQuery and AJAX to update the current HTML content only when the image has finished loading

I am attempting to load a .php file that contains a 2Mb image into a specific div. While I can make this work with ajax, my issue is that I only want the content of the current div to be replaced with the new one once the image finishes loading. Currently ...

Steps for accessing the value from an input tag within the same form:

Exploring how to utilize the value "typed_from_user" in another tag within the same form, using only PHP & HTML without relying on JavaScript. Is this scenario feasible? For instance, let's say I wish to reuse the term 'pizza' <form> ...

How can I transfer a particular data value from a div to JavaScript within Laravel 5.0?

Displaying separate square divs based on integers retrieved from the database. This is the front-end view. I want to pass the room ID (code) to a JavaScript function when clicking on these div elements. https://i.stack.imgur.com/aIYTr.png Below is my cur ...

Are push notifications supported in Ionic3?

I've been struggling to set up push notifications in my Ionic3 app for the past couple of days, and no matter what I try, it doesn't seem to work due to the current versions I'm using. Here are my current versions: rxjs: 5.5.11 Angular: 5 ...

Creating a test suite with Jasmine for an Angular ui-grid component compiled without using $scope

I have encountered an issue while using $compile to compile a ui-grid for Jasmine testing. Initially, everything worked smoothly when I passed $scope as a parameter to the controller. However, I am now transitioning to using vm, which has resulted in $comp ...

Switching views in AngularJS by using a controller to control the content displayed in the

My JavaScript web app utilizes AngularJS to simplify tasks, but I've encountered an issue. I'm trying to change views from an ng-controller using $location.path, but for some reason, the view isn't updating even though the path in the $loca ...

Prevent scrolling within input field

I have a text entry field with background images that separate each letter into its own box. Unfortunately, an issue arises when I reach the end of the input: the view automatically scrolls down because the cursor is positioned after the last letter enter ...

Is there a way for me to position my arrow beside the header while still keeping the toggle function intact?

Can anyone assist me in positioning my arrow next to the header text, which I toggle on click? I attempted placing the arrow <div> into the H1 tag, but then the toggle function stops working. Any help would be greatly appreciated! Please includ ...

Position two div elements evenly with text enclosed in a container

I'm looking to incorporate two distinct blocks of text into a container https://i.stack.imgur.com/6mFZK.png Criteria: Text 1: positioned at the top left corner (85% of box size) Text 2: positioned at the bottom right corner (15% of box size) I&ap ...

What is the best way to center my navigation bar without interfering with the mobile version's JavaScript functionality?

Just starting out with web development and stack overflow, so please bear with me if I struggle to explain the issue. I came across some JavaScript to make my website responsive on small screens (mobiles). However, I am having trouble centering my top nav ...

Alter the content of an HTML page depending on whether a user is logged in two times on the same page

Currently, I am in the process of developing a login system for my website. Everything seems to be functioning correctly so far. However, I have an idea to enhance the user experience by displaying their name with a 'Manage' button next to it whe ...

Guide on deploying an Angular 7 frontend paired with a Spring Boot backend

Having trouble with integrating my Angular application with Spring Boot REST backend. Can anyone suggest a simple method to run both on the same localhost port? ...

How to horizontally center a div between two floated elements

Check out this JSFiddle example. I'm facing a challenge in horizontally centering the div class="filter-group" between two floated buttons within the given example. I've tried various methods but haven't been successful so far. Any assistan ...

Angular: Merge two Observables to create a single list and fetch the combined data

I am currently working on creating a function that returns an observable with a list, compiled from several observables. I feel like I am very close to finding the solution because the debugger stops right before displaying the list. Here is my code: ts t ...

Mastering the Material-UI Grid in SPAs: Conquering the Negative Margin Dilemma

While attempting to build a single page application (SPA), I've encountered an issue with using the Grid component from material-ui. Normally, I rely heavily on the Grid, but in this new project, something seems amiss. The current problem is evident ...