Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular 7 redirecting to another page

I have a login page and a dashboard component.My problem is when i login from the page the dashboard displays below the login page, it is not redirecting as a new page.How to achieve this in angular 7? Any help would be helpful'. Thanks!!

app.component.ts

import { Component } from '@angular/core';
import { Router } from '@angular/router';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent {
  title = 'Shopping-cart';
  user = []
  users = []
  public Candidate = []
  public showWhen:boolean = false;

  constructor(private _router: Router){}

  authenticateLogin(user){
    let authUser = JSON.parse(localStorage.getItem('auth'))
    console.log(user);
    if(user.mail === authUser[0] && user.password === authUser[1]){
      this.showWhen = true;
      console.log("Logged in Successfully");
      this._router.navigate(['dashboard']);
    } else {
      console.error("Invalid email and password");
    }
  }

}

This is my routing-module

app-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { DashboardComponent } from './dashboard/dashboard.component';

const routes: Routes = [
  { path: 'dashboard', component: DashboardComponent} 
];

@NgModule({
  imports: [RouterModule.forRoot(routes, { enableTracing: true })],
  exports: [RouterModule]
})
export class AppRoutingModule { }
like image 254
Ben Avatar asked Jan 18 '19 10:01

Ben


People also ask

How do I navigate to another page in angular 10?

Navigating Programatically Using Angular 10 Router. navigate() and Router. navigateByUrl() The Angular 10 Router provides two methods that you can use to navigate to other components in your component class instead of using the RouterLink directive in the template.


1 Answers

In Routes you have only one route - to dashboard - make second route to login

like image 161
Kamil Kiełczewski Avatar answered Oct 18 '22 21:10

Kamil Kiełczewski