angular 7 重定向到另一个页面

angular 7 redirecting to another page

我有一个登录页面和一个仪表板 component.My 问题是当我从仪表板显示在登录页面下方的页面登录时,它没有重定向为新的 page.How 以实现此目的angular7?任何帮助都会有所帮助'。谢谢!!

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");
    }
  }

}

这是我的路由模块

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

Routes 中,您只有一条路线 - 到仪表板 - 使用第二条路线登录

也许对于登录表单,创建您自己的组件并将其添加到路由中。

您应该有另一个到登录页面的路径。没有它,您将无法对登录页面进行任何调用。

在你的代码部分, 试试这些代码块

app.component.ts

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 {
      this._router.navigate(['login']);
    }
  }

你应该在

中有 另一个路由器路径

app-routing.module.ts

const routes: Routes = [
  { path: 'dashboard', component: DashboardComponent} ,
  { path: 'login', component: LoginComponent(write your login component name)} 
];