我是使用AngularJs 2和进行Web开发的新手。我也没有AngularJs 1的经验。所以,如果我问一些愚蠢的话,我很抱歉。我遇到了问题,我用Google搜索,但我找不到任何解决方案。 :/
问题是:
我正在尝试在ngOnInit结尾处调用的函数中使用 我在ngOnInit 上初始化的对象的属性,但此对象仍然 undefined ,所以我得到以下异常:
Error: Uncaught (in p**strong text**romise): TypeError: Cannot read property 'secretariatId' of undefined
这是我的组件:
import {Component, OnInit, AfterViewChecked} from '@angular/core';
import { ActivatedRoute, Router, Params } from '@angular/router';
import {StudentService} from './services/student.service';
import {Student} from './services/student';
import {SecretariatService} from './services/secretariat.service';
import {DisciplineService} from './services/discipline.service';
import {Discipline} from './services/discipline';
@Component({
selector: 'fountain-enrollstudent',
template: require('./templates/enrollstudent.component.html')
})
export class EnrollStudentComponent implements OnInit, AfterViewChecked {
public text: string;
selectedDiscipline: Discipline;
disciplines: Discipline[];
student: Student;
constructor(
private disciplineService: DisciplineService,
private studentService: StudentService,
private secretariatService: SecretariatService,
private router: Router,
private route: ActivatedRoute
) {
this.text = 'My brand new component!';
}
ngOnInit(): void {
this.route.params.forEach((params: Params) => {
console.log(params);
let id = +params['id'];
this.getStudent(id);
console.log(id);
console.log(this.student);
this.getDisciplines(this.student.secretariatId);
});
console.log(this.student);
}
// Understand the best moment to call it. Ps.: It doesn't work on ngOnInit
getDisciplines(secretariatId: number): void {
this.disciplineService.getDisciplinesBy(secretariatId)
.then(disciplines => this.disciplines = disciplines);
}
getStudent(id: number): void {
this.studentService.getStudent(id)
.then(student => this.student = student);
}
onSelect(discipline: Discipline): void {
this.selectedDiscipline = discipline;
}
}
这是我的模板:
<div class="text-center">
<h4>Disciplines:</h4>
<ul class="list-unstyled">
<li *ngFor="let discipline of disciplines" (click)="onSelect(discipline)" [class.selected]="discipline === selectedDiscipline">
<h4>
<a>{{discipline?.name}}</a>
</h4>
</li>
</ul>
</div>
这是DisciplineService:
import { Injectable } from '@angular/core';
import { Discipline } from './discipline';
import { DISCIPLINES } from './mock-disciplines';
@Injectable()
export class DisciplineService {
getDisciplines(): Promise<Discipline[]> {
return Promise.resolve(DISCIPLINES);
}
getDisciplinesBy(secretariatId: number): Promise<Discipline[]> {
return this.getDisciplines()
.then(disciplines => disciplines.filter(discipline => discipline.secretariatId === secretariatId));
}
}
我正在寻找另一个Lifecycle Hooks来调用函数getDisciplines(id:number),但我尝试了所有这些并且我没有取得好成绩。 :/
提前致谢!
答案 0 :(得分:3)
您需要链接异步调用:
ngOnInit(): void {
this.route.params.forEach((params: Params) => {
console.log(params);
let id = +params['id'];
// vvvvvvvv use `.then(...)`
this.getStudent(id).then(student => {
console.log(id);
console.log(this.student);
this.getDisciplines(this.student.secretariatId);
});
}
}
getStudent(id: number): void {
// vvvvvvvv return the promise so callers can chain their code
return this.studentService.getStudent(id)
.then(student => this.student = student);
}