【问题标题】:angular 7 redirecting to another page角度 7 重定向到另一个页面
【发布时间】:2020-01-27 18:29:44
【问题描述】:

我有一个登录页面和一个仪表板组件。我的问题是当我从页面登录时,仪表板显示在登录页面下方,它没有重定向为新页面。如何在 Angular 7 中实现这一点?任何帮助都会有所帮助'。谢谢!!

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 你只有一条路线 - 到仪表板 - 让第二条路线登录
  • @KamilKiełczewski 是的,你的权利!现在它重定向到仪表板页面。感谢您的帮助。
  • 我接受了你的回答。我的投票没有显示,因为我的声望少于 15 个!!

标签: typescript angular-routing angular7


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您应该有另一条通往登录页面的路线。没有它,您将无法对登录页面进行任何调用。

    在您的代码中, 试试这些代码块

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 是的,我只是这样做了,问题是我没有将它添加到路线中
      猜你喜欢
      • 1970-01-01
      • 2020-09-08
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 2014-01-22
      • 2016-12-14
      • 1970-01-01
      相关资源
      最近更新 更多