简体   繁体   English

Angular 带有导航栏的登录页面 - 事件顺序问题

[英]Angular Login Page with Navbar - Issue with order of events

I'm using Angular 9 and I've used the boiler plate sidenav Angular Material template.我正在使用 Angular 9 并且我使用了样板 sidenav Angular 材料模板。 I'm forcing the sidenav to be open all the time when in larger size screens and a hamburger icon when in smaller sizes.我强迫sidenav在大尺寸屏幕上一直打开,在小尺寸屏幕上打开汉堡图标。

During the login process, when a user isn't authenticated, the sidenav and hamburger icon are hidden with an *ngIf.在登录过程中,当用户未通过身份验证时,sidenav 和汉堡包图标将使用 *ngIf 隐藏。 I also use a template ref variable #drawer on the sidenav container to be referenced by the hamburger icon (click)="drawer.toggle()"我还在 sidenav 容器上使用模板引用变量#drawer,以供汉堡图标 (click)="drawer.toggle()" 引用

The problem I believe is because I'm not showing these items before a login happens, the template ref variable never gets set at the outset of the component creation.我相信的问题是因为我没有在登录发生之前显示这些项目,模板 ref 变量永远不会在组件创建开始时设置。 So, when I try to use the hamburger icon to toggle the sidenav I get a don't understand toggle of undefined error.因此,当我尝试使用汉堡图标来切换 sidenav 时,我得到一个不理解未定义错误的切换。

My login component is being shown in a router outlet so it has to stay in this config.我的登录组件显示在路由器插座中,因此它必须保留在此配置中。 Ideally I'd show a login component first, then go to the app when a user is logged in, but I'm not sure how to do that.理想情况下,我会先显示一个登录组件,然后在用户登录时向应用程序显示 go,但我不知道该怎么做。 Looking for a little guidance on this issue if anyone can help.如果有人可以提供帮助,请在此问题上寻求一些指导。

Thanks so much非常感谢

---- NAVIGATION COMPONENT ---- ---- 导航组件 ----


<mat-sidenav-container class="sidenav-container">
  <mat-sidenav *ngIf="securityObject.isAuthenticated" #drawer class="sidenav" fixedInViewport="true"
    [attr.role]="isHandset ? 'dialog' : 'navigation'" [mode]="(isHandset | async)!.matches ? 'over' : 'side'"
    [opened]="!(isHandset | async)!.matches">
    <mat-toolbar color="primary">Menu</mat-toolbar>
    <!-- SIDEBAR NAVIGATION CONTENT -->
    <mat-nav-list>
      <a mat-list-item routerLink="/users" routerLinkActive="active">
        <mat-icon matListIcon>people</mat-icon>Users
      </a>
      <a mat-list-item routerLink="/clients" routerLinkActive="active">
        <mat-icon matListIcon>business</mat-icon>Clients
      </a>
      <a mat-list-item routerLink="/documents" routerLinkActive="active">
        <mat-icon matListIcon>folder</mat-icon>Documents
      </a>
      <a mat-list-item routerLink="/projects" routerLinkActive="active">
        <mat-icon matListIcon>apps</mat-icon>Projects
      </a>
      <a mat-list-item routerLink="/facility" routerLinkActive="active">
        <mat-icon matListIcon>storefront</mat-icon>Facility
      </a>
      <a mat-list-item routerLink="/tools" routerLinkActive="active">
        <mat-icon matListIcon>build</mat-icon>Tools
      </a>
    </mat-nav-list>
  </mat-sidenav>
  <mat-sidenav-content>
    <mat-toolbar color="primary">
      <ng-container *ngIf="securityObject.isAuthenticated">
        <button type="button" aria-label="Toggle sidenav" mat-icon-button (click)="drawer.toggle()"
          *ngIf="(isHandset | async)!.matches">
          <mat-icon aria-label="Side nav toggle icon">menu</mat-icon>
        </button>
      </ng-container>
      <span>WWP&S Portal</span>
      <span class="example-fill-remaining-space"></span>
      <span><a routerLink="/logout" (click)="logout()" *ngIf="securityObject.isAuthenticated">
          {{ this.securityService.securityObject.username }}</a></span>
    </mat-toolbar>
    <ng-content></ng-content>
  </mat-sidenav-content>
</mat-sidenav-container>

--- NAVIGATION TS FILE --- --- 导航 TS 文件 ---


import { Component, OnInit, ViewChild } from "@angular/core";
import { Observable } from "rxjs";
import {
  BreakpointState,
  BreakpointObserver,
  Breakpoints,
} from "@angular/cdk/layout";
import { SecurityService } from "../../services/security.service";
import { UserAuth } from "../../models/user-auth";

@Component({
  selector: "app-navigation",
  templateUrl: "./navigation.component.html",
  styleUrls: ["./navigation.component.css"],
})
export class NavigationComponent implements OnInit {
  drawer;
  securityObject: UserAuth = null;
  isHandset: Observable<BreakpointState> = this.breakpointObserver.observe(
    Breakpoints.Handset
  );
  constructor(
    private breakpointObserver: BreakpointObserver,
    public securityService: SecurityService
  ) {}

  ngOnInit() {
    this.securityObject = this.securityService.securityObject;
  }

  logout(): void {
    this.securityService.logout();
  }
}


Try removing drawer property from your typescript code please.请尝试从您的 typescript 代码中删除抽屉属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM