2016-05-01 40 views
0

因此,我一直在構建一個應用程序來了解Angular 2並在終端中遇到此問題:Angular 2錯誤:'/ detail /:id'與現有路由衝突'/ detail /:id'

app/teacher-detail.component.ts(17,3): error TS2369: A parameter property is only allowed in a constructor implementation. 
    [0] app/teacher-detail.component.ts(18,3): error TS2369: A parameter property is only allowed in a constructor implementation. 
    [0] app/teacher-detail.component.ts(23,19): error TS2339: Property '_routeParams' does not exist on type 'TeacherDetailComponent'. 
    [0] app/teacher-detail.component.ts(24,8): error TS2339: Property '_teacherService' does not exist on type 'TeacherDetailComponent'. 

現在,我似乎無法找到問題。我的其他列表(student-detail.component.ts)格式與此列表幾乎完全相同,但沒有錯誤彈出。

import {Component, Input, OnInit} from 'angular2/core'; 
import { RouteParams } from 'angular2/router'; 

import {Teacher} from './teacher'; 
import { TeacherService } from './teacher.service'; 

@Component({ 
    selector: 'my-teacher-detail', 
    templateUrl:'app/teacher-detail.component.html', 
}) 

export class TeacherDetailComponent implements OnInit { 
@Input() teacher: Teacher; 


contructor(
    private _teacherService: TeacherService, 
    private _routeParams: RouteParams) { 

    } 

ngOnInit() { 
    let id = +this._routeParams.get('id'); 
    this._teacherService.getTeacher(id) 
     .then(teacher => this.teacher = teacher); 
} 
goBack() { 
window.history.back(); 
} 

}

展望Chrome瀏覽器開發工具,我看到:

EXCEPTION: Error: Uncaught (in promise): EXCEPTION: Error during instantiation of Router! (RouterLink -> Router). 
ORIGINAL EXCEPTION: Configuration '/detail/:id' conflicts with existing route '/detail/:id' 
ORIGINAL STACKTRACE: 
Error: Configuration '/detail/:id' conflicts with existing route '/detail/:id' 

這讓我覺得,這是在我的@RouteConfig app文件?

@RouteConfig([ 
{ 
    path: '/dashboard', 
    name: 'Dashboard', 
    component: DashboardComponent, 
    useAsDefault: true 
}, 
{ 
    path: '/detail/:id', 
    name: 'TeacherDetail', 
    component: TeacherDetailComponent 
}, 
{ 
    path: '/detail/:id', 
    name: 'StudentDetail', 
    component: StudentDetailComponent 
}, 
{ 
    path: '/teachers', 
    name: 'Teachers', 
    component: TeachersComponent 

}, 
{ 
    path: '/students', 
    name: 'Students', 
    component: StudentsComponent 
}, 
]) 

我完全卡住了,無法弄清楚問題是什麼。我懷疑它與/ detail/id有關,但不知道如何繼續,因爲刪除其中的一個並不能解決整體問題。

感謝您的幫助提前!

+1

那麼,你必須至少刪除一個映射:你不能有兩個不同的路線具有完全相同的URL。當你這樣做時會發生什麼? –

+0

好的,所以我刪除了帶有StudentDetail名稱的/ detail/id。我現在可以從儀表板導航到老師,但很明顯,導航到學生鏈接不會回來。 如果我刪除教師版本,單擊學生鏈接仍然沒有返回任何東西(可能是一個不同的錯誤,稍後會檢查) 但是,我仍然遇到同樣的錯誤,顯示在終端中。 – Nathan

+1

錯字:'構造函數'應該是'構造函數'。投票結束爲錯字 –

回答

1

好的,感謝JB Nizet幫助我。

今天我學到了我不能在路由器中有兩條路徑相同的路徑。我將不得不找出一種不同的方式來路由這個應用程序的特定部分。

另外...他發現一個錯字(構造函數而不是構造函數)。

是的,額頭碰到了桌子。幾次。昨晚盯着三小時的代碼,我從來沒有抓到它。

感謝您的幫助JB Nizet。