Я хочу передать значение между компонентами, чтобы переключиться из списка кандидатов на другую панель, где я могу редактировать выбранного кандидата.
К сожалению, я получаю эту ошибку: ОШИБКА TypeError: "this.listCandidateComponent не определен "в моем компоненте редактировать-кандидата, когда я пытаюсь зарегистрировать кандидата, инициализированного в компоненте-кандидате.
список-кандидатов.component.html
<table class="table table-striped">
<tbody *ngFor="let candidate of candidates">
<td><h4>{{ candidate.id }}</h4></td>
<td><a class="btn btn-outline-warning btn-sm" style="margin: 1%"
(click)="getCandidateById(candidate.id)" role="button">Modifier</a>
</td>
</tbody>
</table>
списоккандидат.component.ts
@Component({
selector: 'app-list-candidate',
templateUrl: './list-candidate.component.html',
styleUrls: ['./list-candidate.component.scss']
})
export class ListCandidateComponent implements OnInit {
candidate: Candidate;
candidates: Candidate[];
ngOnInit() {
this.getCandidateList();
}
async getCandidateById(id: number) {
const headers = new HttpHeaders({
'Content-type': 'application/json; charset=utf-8',
Authorization: 'Bearer ' + this.cookie.get('access_token')
});
const options = {
headers
};
await this.httpClient.get(`${this.baseUrl}/${id}`, options)
.toPromise()
.then(
(response: Candidate) => {
console.log('GET request successful', response);
this.candidate = response;
},
(error) => {
console.log('GET error : ', error);
}
);
await this.router.navigate(['/candidates/edit']);
}
edit-candid.component.ts
@Component({
selector: 'app-edit-candidate',
templateUrl: './edit-candidate.component.html',
styleUrls: ['./edit-candidate.component.scss']
})
export class EditCandidateComponent implements OnInit, AfterViewInit {
candidate: Candidate;
@ViewChild(ListCandidateComponent) listCandidateComponent;
ngAfterViewInit() {
this.candidate = this.listCandidateComponent.candidate;
console.log(this.candidate);
}
ngOnInit() {
}
Есть идеи, почему?