Pascal
Pascal

Reputation: 12855

Cannot match any routes with child routes and new angular 2 RC1 router

ApplicationComponent

import { Component } from '@angular/core';
import {Router, ROUTER_DIRECTIVES, Routes, ROUTER_PROVIDERS} from '@angular/router';
import {SchoolyearsComponent} from "./schoolyear/schoolyears.component";

@Component({
  directives: [ROUTER_DIRECTIVES],
  providers: [
    ROUTER_PROVIDERS
  ],
  templateUrl: './app/application.component.html',
  styleUrls: ['./app/application.component.css']
})
@Routes([
  {
    path: '/',
    component: SchoolyearsComponent,
  },
])
export class ApplicationComponent {}

SchoolyearsComponent

import { Component } from '@angular/core';
import { Routes, ROUTER_DIRECTIVES } from '@angular/router';
import { SchoolyearsHomeComponent } from './schoolyears.home.component';
import { CreateSchoolyearComponent } from './create.schoolyear.component';

@Routes([
    {
        path: '',
        component: SchoolyearsHomeComponent,
    },
    {
        path: '/create',
        component: CreateSchoolyearComponent
    }
])
@Component({ template: `<router-outlet></router-outlet>`, directives: [ROUTER_DIRECTIVES]})
export class SchoolyearsComponent {
}

schoolyears.component.html

<h3>Schoolyears</h3>

<div>
<a [routerLink]="['/create']">Create</a>
</div>

<table>
    <tr *ngFor="let s of schoolyears" (click)="createSchoolyear()">
        <td>{{s.id}}</td>
        <td>{{s.name}}</td>
        <td>{{s.startDate}}</td>
        <td>{{s.endDate}}</td>
    </tr>
</table>

When I click on the "Create" routerLink I get this error:

Error

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

Why is the child route not loaded? Why is the /create route not in the available array of routes?

Upvotes: 7

Views: 11911

Answers (4)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657308

Update

This is not relevant anymore in the new V3-beta.2 router.

Original

Change

@Routes([
  {path: '', component: SchoolyearsHomeComponent},
  {path: '/create', component: CreateSchoolyearComponent}
])

to

@Routes([
  {path: '/create', component: CreateSchoolyearComponent},
  {path: '', component: SchoolyearsHomeComponent},
])

The order of routes is currently significant. The most specific routes should come first, the least specific routes last. This is a known issue and should be fixed soon.

Upvotes: 7

Ravali
Ravali

Reputation: 1

Looks like the order of routes we mention in @Routes is notable. The most specific routes should come first, the least specific routes last.Depending on it change your @Routes to this

@Routes([
  {path: '/create', component: CreateSchoolyearComponent},
  {path: ' ', component: SchoolyearsHomeComponent},
])`

Upvotes: 0

Gallaecio
Gallaecio

Reputation: 3857

You must remove the leading '/', the new router handles it for you.

@Routes([
  {path: 'create', component: CreateSchoolyearComponent},
  {path: '', component: SchoolyearsHomeComponent},
])

Upvotes: 3

DeborahK
DeborahK

Reputation: 60518

You need to inject the router into the app component, something like this:

 export class AppComponent {

  constructor(private router: Router) {}

}

This is normally needed when the template associated with this component does not use the <router-link> directive. A recent change to Angular does not load the router component when only using the <router-outlet>

Upvotes: 0

Related Questions