2016-06-01 7 views
7

ApplicationComponent子ルートを持つ任意のルートを照合して、新しい角度2 RC1ルータはできません

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> 

私はrouterLinkを「作成」をクリックすると、私はこのエラーを取得:

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

エラーがなぜロードされていない子のルートですか? /createルートが利用可能なルートの配列にないのはなぜですか?

答えて

7

を使用した場合の角への最近の変更は、ルータコンポーネントをロードしない更新

これは、新しいV3-beta.2ルータにはもう関係ありません。

オリジナル

変更

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

ルートの順

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

には現在、重要です。 最も特定のルートが最初に来て、最も特定のルートが最後に来るべきです。 これは既知の問題であり、間もなく修正する必要があります。

+0

これは役に立たなかった。同じエラーメッセージが表示されます。現在のセグメンテーション "create"は利用可能なルートにありません。あなたは私の目標を知っているようでしたが、確かめるために:「createy」URLに移動して、schoolyears.component.htmlをcreate.schoolyear.component.htmlに置き換えたいと思います。 – Pascal

+0

https://plnkr.co/edit/oksKwNmGvubDlpV45yEB?p=previewで問題なく動作しているようです。このPlunkerで問題を再現しようとすることはできますか? –

+0

私はrouterLink(サンプルで忘れてしまった)を正しい場所に追加したいと思う:https://plnkr.co/edit/vjCbsqZazr0rp77xjnSm?p=previewそれをクリックすると何も起こらない?私は私のリンクでどうぞ。 – Pascal

0

あなたは、アプリケーションのコンポーネントには、このような何かルータを注入する必要があります。このコンポーネントに関連付けられているテンプレートは<router-link>ディレクティブを使用しない場合に

export class AppComponent { 

    constructor(private router: Router) {} 

} 

これは通常必要とされています。唯一<router-outlet>

+0

これは役に立ちませんでした。同じエラーメッセージが表示されます。現在のセグメンテーション "create"は利用可能なルートにありません。 – Pascal

3

先頭の '/'を削除する必要があります。新しいルータがそれを処理します。

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

私はこれをテストしたが、これは正しいと思われる! –

0

@Routesに記載されている経路の順番がわかります。最も具体的なルートが最初に来て、最後に最小のルートが最後に来るようにする必要があります。これであなたの@Routesをこれに変更します。

関連する問題