I've added a login feature to my app. When someone navigates to the URL with a blank path(http://example.com), I want the HomeComponent to show up which will activate the AuthGuard, but keep the URL path blank. I am still testing locally, so that may be the issue? I'm not sure.
Whats happening now - when I navigate to localhost:3000/ I get nothing, just a blank screen. When I navigate to localhost:3000/home, I get my HomeComponent to show with no issue but I would like the HomeComponent to be called when the URL is localhost:3000/.
Here's my app.module file - I'm not really sure what other files I need to post since the issue lies with the routing. Like I said, I can get to all of the other components by typing the path. But for Home, I do not want to have to type the path.
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { RouterModule, Routes } from '@angular/router';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { LoginComponent } from './login/login.component';
import { AlertService } from './login/services/alert.service';
import { AlertComponent } from './directives/alert.component';
import { AuthenticationService } from './login/services/authentication.service';
import { AuthGuard } from './guards/auth.guard';
@NgModule({
imports: [
BrowserModule,
FormsModule,
ReactiveFormsModule,
HttpModule,
RouterModule.forRoot([
{ path: '', component: HomeComponent, pathMatch: 'full', canActivate: [AuthGuard] },
{ path: 'home', component: HomeComponent, canActivate: [AuthGuard] },
{ path: 'login', component: LoginComponent },
{ path: '**', redirectTo: '', pathMatch: 'full' }
])
],
providers: [
AuthGuard,
AlertService,
AuthenticationService
],
declarations: [
LoginComponent,
AlertComponent,
HomeComponent,
AppComponent,
],
bootstrap: [ AppComponent ]
})
export class AppModule { }
I have also tried setting my default route like this, but still same issue:
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent, pathMatch: 'full', canActivate: [AuthGuard] },
My base href is set to:
<base href="/" />
I'm not receiving any errors and my console looks clean. Again, I can navigate just fine if I add "/home" to the end of my URL, but I want a blank URL path to show the HomeComponent with AuthGuard.
I've been through documentation and other questions but nothing has worked for me. I'm not sure what I'm missing. Any help would be greatly appreciated. Let me know if there are any other code snippets I need to post. Thanks in advance.