Snorlax
Snorlax

Reputation: 4745

angular: routing isn't showing my component properly

My app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';


import { AppComponent } from './app.component';
import { LoginComponent } from './login/login/login.component';
import { AppRoutingModule } from './app-routing.module';


@NgModule({
  declarations: [
    AppComponent,
    LoginComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

My app-routing.module:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule, Routes } from '@angular/router';
import { LoginComponent } from './login/login/login.component';

export const appRoutes: Routes = [
  { path: '', component: LoginComponent},
  {
      path: 'ok',
      component: LoginComponent,
  },
  // otherwise redirect to home
  { 
    path: '**', 
    component: LoginComponent 
  }
];

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

I want to show my login component as a home page, and whatever path I pick will also redirect to my login component. I tried putting my logincomponent in bootstrap in my app.module.ts but it gave me an error.

My login component is a simple <p>login works!</p>

Upvotes: 0

Views: 123

Answers (2)

Vikas
Vikas

Reputation: 12036

Modify your Routes add a redirect route and pathMatch property to tell the router how to match a URL to the path of a route

export const appRoutes: Routes = [
     { path: '',   redirectTo: '/ok', pathMatch: 'full' },
      {
          path: 'ok',
          component: LoginComponent,
      },
      // otherwise redirect to home
      { 
        path: '**', 
        component: LoginComponent 
      }
    ];

Upvotes: 0

Sajeetharan
Sajeetharan

Reputation: 222582

You have to configure the routes as follows,

 imports: [
    RouterModule.forRoot([
      { path: '', component: LoginViewComponent },
      { path: 'ok', component: LoginViewComponent },
      { path: '**', redirectTo: 'login' }
    ])
  ]

STACKBLITZ DEMO

Upvotes: 1

Related Questions