所以我一直在构建一个应用程序来了解Angular 2并在终端遇到这个问题:
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的应用文件中吗?
@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有关,但不确定如何继续,因为删除其中一个并不能解决整个问题。
提前感谢您的帮助!
答案 0 :(得分:1)
好的,感谢JB Nizet帮助我。
今天我了解到我在路由器中不能有两条路径相同的路径。我将不得不找出一种不同的方式来路由应用程序的这个特定部分。
另外......他发现了一个拼写错误(构造函数而不是构造函数)。
是的,额头见了桌子。几次。昨晚盯着代码三个小时,我从来没有抓住它。
感谢JB Nizet的帮助。