Angular 7 - 成功登录后显示用户名
创始人
2024-10-16 14:02:06
0

可以通过使用Angular的路由守卫和服务来实现在成功登录后显示用户名的功能。下面是一个示例代码:

  1. 创建一个名为auth.service.ts的服务,用于处理用户认证和保存用户信息:
import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class AuthService {
  private isAuthenticated: boolean = false;
  private username: string = '';

  constructor() { }

  login(username: string, password: string): boolean {
    // 实际的认证逻辑
    // 如果认证成功,则设置isAuthenticated为true,并保存用户名
    this.isAuthenticated = true;
    this.username = username;
    return true;
  }

  logout(): void {
    // 登出逻辑
    // 设置isAuthenticated为false,并清除用户名
    this.isAuthenticated = false;
    this.username = '';
  }

  isLoggedIn(): boolean {
    return this.isAuthenticated;
  }

  getUsername(): string {
    return this.username;
  }
}
  1. 创建一个名为auth.guard.ts的路由守卫,用于检查用户是否已经登录:
import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';
import { AuthService } from './auth.service';

@Injectable({
  providedIn: 'root'
})
export class AuthGuard implements CanActivate {

  constructor(private authService: AuthService, private router: Router) { }

  canActivate(): boolean {
    if (this.authService.isLoggedIn()) {
      return true;
    } else {
      this.router.navigate(['/login']);
      return false;
    }
  }
}
  1. 在需要显示用户名的组件中,使用AuthService来获取用户名并显示:
import { Component, OnInit } from '@angular/core';
import { AuthService } from './auth.service';

@Component({
  selector: 'app-username',
  template: '

Welcome, {{ username }}

' }) export class UsernameComponent implements OnInit { username: string = ''; constructor(private authService: AuthService) { } ngOnInit() { this.username = this.authService.getUsername(); } }
  1. 在路由配置中使用AuthGuard来保护需要登录才能访问的路由:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AuthGuard } from './auth.guard';
import { LoginComponent } from './login.component';
import { UsernameComponent } from './username.component';

const routes: Routes = [
  { path: 'login', component: LoginComponent },
  { path: 'username', component: UsernameComponent, canActivate: [AuthGuard] },
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

这样,在成功登录后,用户将被重定向到/username路由,并且UsernameComponent将显示用户的用户名。

相关内容

热门资讯

透视了解!拱趴大菠萝有什么挂(... 透视了解!拱趴大菠萝有什么挂(透视)aa poker辅助包,教程方式(有人有挂)-哔哩哔哩1、这是跨...
智星德州可以透视吗!wepok... 智星德州可以透视吗!wepoker私人局规律(透视)挂-一贯科普真的是有挂1、每一步都需要思考,不同...
透视曝光!wejoker辅助机... 透视曝光!wejoker辅助机器人(透视)pokemmo脚本辅助器下载,教程要领(了解有挂)-哔哩哔...
fishpoker透视!aap... fishpoker透视!aapoker怎么设置提高好牌几率(透视)挂-都是曝光是真的挂该软件可以轻松...
透视分享!红龙poker辅助器... 透视分享!红龙poker辅助器免费观看(透视)hhpoker有没有开挂,教程方针(有人有挂)-哔哩哔...
wepoker辅助真的假的!w... wepoker辅助真的假的!wepoker透视脚本免费下载(透视)软件-原来开挂有挂wepoker透...
透视揭露!智星菠萝可以辅助吗(... 透视揭露!智星菠萝可以辅助吗(透视)wepoker破解器激活码,教程指引(发现有挂)-哔哩哔哩1、用...
wpk透视!wpk软件是正规的... wpk透视!wpk软件是正规的吗(透视)技巧-切实普及有挂1、起透看视 wpk软件是正规的吗辅助软件...
透视揭幕!wepoker辅助器... 透视揭幕!wepoker辅助器如何使用(透视)哈糖大菠萝有挂吗,教程妙计(有挂细节)-哔哩哔哩1、w...
wepoker轻量版辅助!hh... wepoker轻量版辅助!hhpoker开挂靠谱吗(透视)方法-切实详细存在有挂暗藏猫腻,小编详细说...