2016-01-08 53 views
2

我對Angular 2.0非常新(誰不是!?!?!),我無法讓我的路由工作!我不斷收到錯誤'Component "LoginComponent" has no route config. in [['/Register'] in LoginComponent'。組件沒有路由配置。在組件中 - 角2路由錯誤

這是我@RouteConfig和引導代碼(證明我注入依賴關係)從我boot.ts文件採取

bootstrap(LoginComponent, [ 
    ROUTER_PROVIDERS, HTTP_PROVIDERS 
]); 

@RouteConfig([ 
    {path: '/login', name: 'Login', component: LoginComponent, useAsDefault: true}, 
    {path: '/register', name: 'Register', component: RegisterComponent} 
]) 

這是我的LoginComponent代碼...從我login.component.ts文件

import {Component} from 'angular2/core'; 
import {FORM_DIRECTIVES, FormBuilder, ControlGroup, Validators} from 'angular2/common'; 
import {RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS} from 'angular2/router'; 
import {Http, Headers, Response} from "angular2/http"; 

@Component({ 
    selector: 'login', 
    templateUrl: 'app/login/login.component.html', 
    directives: [FORM_DIRECTIVES, ROUTER_DIRECTIVES] 
}) 

export class LoginComponent { 

    loginForm:ControlGroup; 
    http: Http; 
    userNotFound: boolean; 

    constructor(fb:FormBuilder, http: Http) { 
     this.loginForm = fb.group({ 
      'email': ['', Validators.required], 
      'password': ['', Validators.required] 
     }); 

     this.http = http; 
     this.userNotFound = false; 
    } 

    login(event: Event, val:string):void { 
     // stuff happens here 
    } 

    forgotPassword() { 
     // stuff happens here 
    } 
} 

在我看來,我有一個鏈接到我的應用程序的不同部分。

例外:在瀏覽器中,我得到了以下錯誤從我login.component.html文件採取

<a (click)="forgotPassword()">Forgot Password</a> 
<br> 
<a [routerLink]="['/Register']">Register</a> 
<br> 
<div class="error" *ngIf="userNotFound"> 
    User not found. Please try again... 
</div> 

但是組件「LoginComponent」沒有路線配置。在LoginComponent的[['Register'中] @ 23:19] 原文例外:組件「LoginComponent」沒有路由配置。 ORIGINAL STACKTRACE: 錯誤:組件「LoginComponent」沒有路由配置。

任何人都可以看到究竟是什麼問題嗎?默認的作品和一切都很好,直到我介紹<a [routerLink]="['/Register']">Register</a>超鏈接。我一直在努力工作一段時間!

這裏是我的項目結構(如果它幫助)...

project structure

+0

其中是你的'',第二件事angular2期望從LoginComponent路由但無法找到。 –

+1

你正在引導錯誤的組件。因此,當你編寫'/ SomeComponent'(絕對路徑)時,你的根組件是'LoginComponent',它將從沒有RouteConfig的'LoginComponent'開始。 –

+0

我有一個類似嵌套路由的問題。關於這個問題的解釋有所幫助。 https://github.com/angular/angular/issues/8420 – theUtherSide

回答

1

其實高達我能理解的問題是路由文件,因爲你是引導登錄組件文件和你正在做的您在boot.ts文件中的路由。而是你必須做的loginComponent文件路由因爲angular2找到你的情況bootstraped文件即路由boot.ts但是當你寫

<a [routerLink]="['/Register']">Register</a> 

這將查找的文件login.component.ts路由。但無法找到我認爲的錯誤。其次是進口,這在我想的問題中沒有提到。

在logincomponent文件中使用你的父路由,我希望這會有所幫助。

+0

我意識到我應該使用routerLink。我已將@RouteConfig移到LoginComponent中 - 我仍然遇到錯誤! –

+0

將@RouteConfig移入logincomponent後出現錯誤?錯誤是相同還是改變了?並檢查registerCompoent的正確導入路徑。它是否正確? –

0

我有一個類似的錯誤,要是你,請將此代碼

@RouteConfig([ 
    {path: '/login', name: 'Login', component: LoginComponent, useAsDefault: true}, 
    {path: '/register', name: 'Register', component: RegisterComponent} 
]) 

LoginComponent文件裏。

import {Component} from 'angular2/core'; 
import {FORM_DIRECTIVES, FormBuilder, ControlGroup, Validators} from 'angular2/common'; 
import {RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS} from 'angular2/router'; 
import {Http, Headers, Response} from "angular2/http"; 

@Component({ 
    selector: 'login', 
    templateUrl: 'app/login/login.component.html', 
    directives: [FORM_DIRECTIVES, ROUTER_DIRECTIVES] 
}) 

export class LoginComponent { 
..// 

import {Component} from 'angular2/core'; 
    import {FORM_DIRECTIVES, FormBuilder, ControlGroup, Validators} from 'angular2/common'; 
    import {RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS} from 'angular2/router'; 
    import {Http, Headers, Response} from "angular2/http"; 

    @RouteConfig([ 
    {path: '/login', name: 'Login', component: LoginComponent, useAsDefault: true}, 
    {path: '/register', name: 'Register', component: RegisterComponent} 
    ]) 

    @Component({ 
     selector: 'login', 
     templateUrl: 'app/login/login.component.html', 
     directives: [FORM_DIRECTIVES, ROUTER_DIRECTIVES] 
    }) 

    export class LoginComponent { 
    ..// 

我希望這可以幫助你,爲我的英語不好對不起。

0

嗨可能有點晚,但我認爲這個問題可能是你的名字引用不正確。鏈接應該如下刪除正斜槓。

<a [routerLink]="['Register']">Register</a>