のプロパティを読み取ることができません:コンポーネントのTSファイルで定義された@Input複雑なオブジェクトは、エラーをスローすると、親コンポーネントに子コンポーネントにパラメータを渡す未定義
<app-conf-name
[inputName]='person.name'
(updatedNameEvent)='updateName($event)'>
</app-conf-name>
@Input() inputName: Names;
@Output() updatedNameEvent: EventEmitter<Names> = new EventEmitter();
constructor() { }
editName: Names = new Names();
ngOnInit() {
this.editName = this.inputName;
}
クラスでは、Names型がcorrectPersonフィールドのデフォルト値を設定します。
export class Names {
indivId: number;
firstName: string;
prefName: string;
lastName: string;
suffix: string;
updated: boolean = false;
correctPerson: boolean = false;
correctAsIs: boolean = false;
correctionDate: Date;
addedDate: Date;
}
は、なぜ私はこのエラーを取得しています:
ERROR TypeError: Cannot read property 'correctPerson' of undefined
at Object.View_ConfNameComponent_0._co [as updateDirectives] (ConfNameComponent.html:9)
at Object.debugUpdateDirectives [as updateDirectives] (core.es5.js:13056)
at checkAndUpdateView (core.es5.js:12236)
at callViewAction (core.es5.js:12601)
at execComponentViewsAction (core.es5.js:12533)
at checkAndUpdateView (core.es5.js:12242)
at callViewAction (core.es5.js:12601)
at execComponentViewsAction (core.es5.js:12533)
at checkAndUpdateView (core.es5.js:12242)
at callViewAction (core.es5.js:12601)
オブジェクトperson
が親コンポーネントに呼び出されたサービスから初期化されます。
親コンポーネント私は割り当てを経由して、エラーをもう一度投げるのプロパティを初期化してきました
person: Person = new Person();
constructor(private peopleService: PeopleService) { }
state: string = 'one';
ngOnInit() {
this.peopleService.getPersonById(this.Id).subscribe((data)=>{
this.person = data;
console.log(this.person);
this.person.name.correctPerson = false;
this.person.name.correctAsIs = false;
this.person.email.correctAsIs = false;
this.person.phone.correctAsIs = false;
this.person.address.correctAsIs = false;
});
}
注意。
子コンポーネントの元のコードでは、入力変数が作業変数として使用されていました。
この質問に答えてプロジェクトの全面的な働きを試してみたところ、ngIfをどこに配置しても最終的な子コンポーネントが最後にエラーを投げかけていたことが分かりました。
はここにあなたがOnInit
ではなくOnChanges
で@Input()
パラメータにアクセスするべきではない完全なGitHub Repo
親コンポーネントのコードとperson.nameの解決方法を教えてください。 –
をリクエストしてくださいJeanPaul –