我傳遞數據到一個可訪問的子組件,我可以使用這些數據構建出我的視圖。但是,我需要訪問@Input對象上的一個屬性來查找具有相同值的數組中的項目。相當簡單直接。當組件加載時,我在構造函數中執行此操作,以便立即可用。@Input()屬性無法訪問,因爲它說的對象在未定義的角2
但是,@Input對象回來undefined所以我不能訪問屬性。有人可以看看並告訴我這裏發生了什麼?
回到未定義的對象是「this.ConfirmOrderData」,就像我說的,我可以做ngFor重複這個對象屬性數組等,所以我很難爲什麼它是未定義的。
CODE:
@Input() ConfirmOrderData:{
orderRefillData: Array<any>,
selectedUser: any,
DeliveryTypeId: any,
PickupLocationId: any,
PaymentTypeId: any,
AddressType: any
};
constructor(
fb: FormBuilder,
public refillService: refillService,
public globalService: globalService
) {
this.userInfo = this.globalService.getUserInfo();
this.deliveryAddress = this.userInfo.userInfo.StreetAddresses.find((item) => item.DeliveryTypeId == this.ConfirmOrderData.AddressType.AddressTypeId)
//the this.ConfirmOrderData object in the above line comes back undefined. Don't know why.
}
當您直接使用屬性在模板中,就像沒有被觀察到的數據一樣,它必須被定義,或者你可以使用?像'data?.item?.couldbelate' – Dylan