28

My component shows up but i get an error message when the page is loaded. I can't seem to solve the error message at all after looking at a bunch of resources.

Error

EXCEPTION: Error: Uncaught (in promise): Cannot match any routes. Current segment: 'index.html'. Available routes: ['/login'].

main.component.ts is in index.html and as soon as the page loads it shows the above error message.

import { Component } from '@angular/core';
import { Routes, ROUTER_DIRECTIVES, ROUTER_PROVIDERS } from '@angular/router';

import { LoginComponent } from './login/login.component';

@Component({
    selector: 'main-component',
    template: 
      ' <header>
           <h1>Budget Calculator</h1>
           <a id='login' [routerLink]="['/login']">Login</a>
           <router-outlet></router-outlet>
        </header> '
    directives: [ROUTER_DIRECTIVES],
    providers: [ROUTER_PROVIDERS]
})

@Routes([
    {path: '/login', component: LoginComponent}
])

export class MainComponent {}

login.component.ts is routed through the main.component.ts and does show when I click on the link despite the error message. Right now I have it styled to popup over the other elements in main.component but would like it to be the only component that shows on the page. Basically replace main.component in index.html with login.component if this possible instead of doing a whole bunch of styling to display: none.

import { Component } from '@angular/core';

@Component({
    selector: 'login',
    template: 
        ' <div id="login-pop-up">
              <h6 class="header_title">Login</h6>
              <div class="login-body">
                   Some fancy login stuff goes here
              </div>
          </div> '
})

export class LoginComponent {}
Grim
  • 2,398
  • 4
  • 35
  • 54

5 Answers5

22

Also ensure you have a <base href="/"> tag at the beginning of the <head> tag or provided by boostrap(...) as explained in Angular 2 router no base href set

Hint: The @angular/router router also is deprecated as @angular/router-deprecated and a new router will be shipped again :-/ If you are about to switch to @angular/router it's probably better to postpone until the new and hopefully final router is available.

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
4

if you have child component and you want to navigate use this way

router servive

const routes: Routes = [
  {
    path: '',
    component: LoginComponent
  },
  {
    path: 'home',
    component: HomeComponent,
    children: [
      {
        path: 'enter',
        component: EnterDetailsComponent,
      },
      {
        path: 'find',
        component: FindDetailsComponent,
      }
    ]
  }
];

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

use navigate([]) method this way

component constructor

this is how this._router comes

constructor( private _router: Router) {}

this._router.navigate(['/home/find']);
Community
  • 1
  • 1
Chanaka Fernando
  • 2,176
  • 19
  • 19
1

When I have had this problem I typically add

<base href="./"> 

to the <head> <base href="./"> </head> of html file.

Then when importing any files in NG2

import { Auth }                 from './auth.service';

This is the prefered way to do base per the documentation, enter image description here

wuno
  • 9,547
  • 19
  • 96
  • 180
0

plz make sure <base href="/"> tag in your head tag and you can use useAsDefault: true attr like this :

@Routes([
    {path: '/login', component: LoginComponent,useAsDefault: true}
])
  • 11
    There is no `useAsDefault` in `@angular/router` :-/ – Günter Zöchbauer Jun 08 '16 at 04:55
  • As far as I know this is not yet implemented in `@angular/router` and won't be because it will be replaced again. Adding it doesn't cause an error but it doesn't have any effect either. Maybe you're using `@angular/router-deprecated`? – Günter Zöchbauer Jun 08 '16 at 05:01
  • yse,i use `@angular/router-deprecated` ,and Whats deference between router and router-deprecated ? –  Jun 08 '16 at 05:14
  • 1
    The difference is that both are deprecated ;-). `@angular/router` is an entirely new implementation that was intended to replace `@angular/router-deprecated` but it didn't fully work out neither and they deprecated it as well. `@angular/router` has several issues and missing features. Currently it's better to stay with `@angular/router-deprecated` until the new new new router is shipped and stable enough. – Günter Zöchbauer Jun 08 '16 at 05:17
  • 1
    tnxx for answer ^-^ –  Jun 08 '16 at 05:51
0

just add a route for '' the exception will go.

{ 
    path: '', 
    component: HomeComponent 
}
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
Sancho George
  • 99
  • 1
  • 4