I successfully developed 4 Angular components:
- 1st component: menu
- 2nd component: header
- 3rd component: home
- 4th component: login
The menu component features a sidebar/navbar created using Material UI. The login component consists of the login page. However, when running ng serve, both the menu and login components are displayed together. My goal is to have only the login component displayed on the initial page redirect. I have set up routing for the login component to appear on the first page, but it currently appears with the menu bar content as well, which can be seen in the image. In app.component.html, I have added
<app-menu></app-menu>
because after logging in, I want the other component data to display when clicking any button on the sidebar.
Below is the code for the menu bar and all the components:
<mat-toolbar color="primary">
<button (click)="drawer.toggle()" mat-icon-button>
<mat-icon>menu</mat-icon>
</button>
<span>Angular</span>
<span class="example-spacer"></span>
<button routerLink="header" mat-button>home</button>
<button routerLink="sidebar" mat-button>About</button>
<button mat-button>contact</button>
<form class="d-flex">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-success" type="submit">Search</button>
</form>
</mat-toolbar>
<mat-drawer-container autosize>
<mat-drawer #drawer opened="true" mode="side" position="start">
<mat-nav-list>
<mat-list-item>
<button routerLink="header" mat-button><mat-icon>home</mat-icon> Home</button>
</mat-list-item>
<mat-list-item>
<button routerLink="home" mat-button><mat-icon>home</mat-icon> Pages</button>
</mat-list-item>
<mat-list-item>
<button routerLink="" mat-button><mat-icon>explore</mat-icon> Table</button>
</mat-list-item>
<mat-list-item>
<button routerLink="#" mat-button><mat-icon>Layout</mat-icon> Layout</button>
</mat-list-item>
<mat-list-item>
<button mat-button><mat-icon>settings</mat-icon> Settings</button>
</mat-list-item>
<mat-list-item>
<button mat-button><mat-icon>help</mat-icon> Help</button>
</mat-list-item>
</mat-nav-list>
</mat-drawer>
<mat-drawer-content>
<div style="text-align: center;min-height: 600px;">
<router-outlet></router-outlet>
</div>
</mat-drawer-content>
</mat-drawer-container>
Login Component Code:
<body>
<form [formGroup] ="loginform1" (ngSubmit)="loginUser()">
<div class="login-container">
<h2 class="lg">Login Page</h2>
<label>User Name</label>
<input type="text" name="Uname" id="Uname" placeholder="Username" formControlName="Uname">
<span style="color: red;" *ngIf="Uname && Uname.invalid && Uname.touched" >this field is required.</span>
<br><br>
<label>Password</label>
<input type="password" name="password" id="Pass" placeholder="Password" formControlName="password">
<span style="color: red;" *ngIf="password && password.invalid && password.touched" >this field is required.</span>
<br>
<a href="#" style="color: white;">Forgot Password</a><br><br>
<button class="bt" (click)="submit()" [disabled]="loginform1.invalid">Login</button> <br><br>
<input type="checkbox" id="check">
<span>Remember me</span><br>
<button type="type" class="btn btn-primary" routerLink="/dashboard">Register</button>
<br><br>
</div>
</form>
</body>
Code in app.component.html:
<app-menu></app-menu>
Routing configuration in app.routing:
{path:'dashboard', component:DashboardComponent},
{path:'header', component:Headers},
{path:'home', component:HomeComponent},
{path:'menu', component:MenuComponent},
{path:'login',component:LoginComponent},
{ path: '', redirectTo: '/login', pathMatch: 'full' },
I aim to have only the login page displayed when running ng serve, while ensuring that all buttons in the sidebar and navbar work correctly with the router link.