Whenever I try to navigate to the home page after logging in, I encounter an issue. I have a navbar
<app-header></app-header>
with two links - one for signing up and another for logging in. After successfully logging in, my goal is to navigate to the home page under the link http://localhost:4200
. In the login.component.ts, I use navigateByUrl() for redirection and the navigation itself works fine. However, the problem arises when my home page does not render properly, showing only a blank white page. Upon investigation, I found that removing <app-header></app-header>
from app.component.html results in the home page rendering correctly, though at the cost of losing the navigation bar. How can I resolve this issue and render my homepage without sacrificing the navigation bar?
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HeaderComponent } from './header/header.component';
import { SignupComponent } from './auth/signup/signup.component';
import { ReactiveFormsModule } from '@angular/forms';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { LoginComponent } from './auth/login/login.component';
import { NgxWebstorageModule } from 'ngx-webstorage';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { ToastrModule } from 'ngx-toastr';
import { TokenInterceptor } from './token-interceptor';
import { HomeComponent } from './home/home.component';
import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';
@NgModule({
declarations: [
AppComponent,
HeaderComponent,
SignupComponent,
LoginComponent,
HomeComponent,
],
imports: [
BrowserModule,
AppRoutingModule,
ReactiveFormsModule,
HttpClientModule,
NgxWebstorageModule.forRoot(),
BrowserAnimationsModule,
ToastrModule.forRoot(),
FontAwesomeModule,
],
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: TokenInterceptor,
multi: true
}
],
bootstrap: [AppComponent]
})
export class AppModule { }
app-routing.module.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { LoginComponent } from './auth/login/login.component';
import { SignupComponent } from './auth/signup/signup.component';
import { HomeComponent } from './home/home.component';
const routes: Routes = [
{path:'', component: HomeComponent},
{path:'signup', component: SignupComponent},
{path:'login', component: LoginComponent},
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
app.component.html
<app-header></app-header>
<router-outlet></router-outlet>
header.component.html
<header>
<nav class="navbar fixed-top navbar-expand-lg navbar-light bg-light">
<div class="flex-grow-1">
<a aria-label="Home" class="logo" routerLink="/">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" class="reddit-icon-svg">
<g>
<circle fill="#FF4500" cx="10" cy="10" r="10"></circle>
<path fill="#FFF"
d="M16.67,10A1.46,1.46,0,0,0,14.2,9a7.12,7.12,0,0,0-3.85-1.23L11,4.65,13.14,5.1a1,1,0,1,0,.13-0.61L10.82,4a0.31,0.31,0,0,0-.37.24L9.71,7.71a7.14,7.14,0,0,0-3.9,1.23A1.46,1.46,0,1,0,4.2,11.33a2.87,2.87,0,0,0,0,.44c0,2.24,2.61,4.06,5.83,4.06s5.83-1.82,5.83-4.06a2.87,2.87,0,0,0,0-.44A1.46,1.46,0,0,0,16.67,10Zm-10,1a1,1,0,1,1,1,1A1,1,0,0,1,6.67,11Zm5.81,2.75a3.84,3.84,0,0,1-2.47.77,3.84,3.84,0,0,1-2.47-.77,0.27,0.27,0,0,1,.38-0.38A3.27,3.27,0,0,0,10,14a3.28,3.28,0,0,0,2.09-.61A0.27,0.27,0,1,1,12.48,13.79Zm-0.18-1.71a1,1,0,1,1,1-1A1,1,0,0,1,12.29,12.08Z">
</path>
</g>
</svg>
<span class="reddit-text">
Spring Reddit Clone
</span>
</a>
</div>
<div class="flex-grow-1 float-right">
<a routerLink="/signup" class="float-right sign-up mr-2">Sign up</a>
<a routerLink="/login" class="float-right login mr-2">Login</a>
</div>
</nav>
</header>
login.component.ts
import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { AuthService } from '../auth.service';
import { LoginRequestPayload } from './login-request.payload';
import { ToastrService } from 'ngx-toastr';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
isError = false;
loginForm: FormGroup;
loginRequestPayload: LoginRequestPayload;
registerSuccessMessage:string;
constructor(private authService: AuthService, private activatedRoute: ActivatedRoute,
private router: Router, private toastr: ToastrService) {
this.loginForm = new FormGroup({
username: new FormControl('', Validators.required),
password: new FormControl('', Validators.required)
});
this.loginRequestPayload = {
username: "",
password: ""
}
this.registerSuccessMessage = "";
}
ngOnInit(): void {
this.activatedRoute.queryParams.subscribe(params =>{
if(params['registered'] !== undefined && params['registered'] === true){
this.toastr.success("Signup Successful");
this.registerSuccessMessage = "Please Check your inbox for activation email and activate your account before you login!"
}
})
}
login(){
this.loginRequestPayload.username = this.loginForm.get('username')?.value;
this.loginRequestPayload.password = this.loginForm.get('password')?.value;
const self = this;
this.authService.login(this.loginRequestPayload).subscribe({
next(response){
console.log(response);
},
complete(){
self.isError = false;
self.toastr.success('Login Successful');
self.router.navigateByUrl('');
},
error(){
group("Error");
self.isError = true;
}
})
}
}