无法读取未定义的 'xxx' 中的 属性
Cannot read property of 'xxx' of undefined
我正在使用 Ionic 2,其中一个组件有两个组件,并且使用发射器共享数据。但是当我执行程序时,就出现了这个错误。
Runtime Error Uncaught (in promise): TypeError: Cannot read property
'BillNo' of undefined TypeError: Cannot read property 'BillNo' of
undefined at Object.eval [as updateDirectives]
这是我的代码:
bill-settlement.html
...
<page-bill-list (BillSelected)="onBillSelected($event)"></page-bill-list>
...
<page-bill-details [billItem]="billItem"></page-bill-details>
...
账单-settlement.ts
@Component({
selector: 'page-bill-settlement',
templateUrl: 'bill-settlement.html',
})
export class BillSettlement {
...
billItem: BillDetail
...
onBillSelected(billData: BillDetail) {
this.billItem = billData
}
}
bill-list.html
<ion-buttons>
<button ion-button *ngFor="let item of billItems" (click)="getBillDetails(item)">
{{item.BillNo}}
</button>
</ion-buttons>
bill-list.ts
@Component({
selector: 'page-bill-list',
templateUrl: 'bill-list.html',
})
export class BillList {
billItems: BillDetail[] = []
billItem = new BillDetail()
@Output() BillSelected = new EventEmitter<BillDetail>()
constructor(public navCtrl: NavController,
public navParams: NavParams,
public billSrv: BillerService,
public authSrv: AuthService,
public genSrv: GenericService) {
this.billSrv.getBills()
.subscribe(data => {
this.billItems = data
})
}
getBillDetails(item: BillDetail) {
this.BillSelected.emit(this.billItem)
}
}
bill-details.ts
@Component({
selector: 'page-bill-details',
templateUrl: 'bill-details.html',
})
export class BillDetails {
...
@Input() billItem: BillDetail
...
}
bill-details.html
...
<ion-input text-right type="text" [value]="billItem.BillNo" readonly></ion-input> //billItem model has BillNo property
...
问题是bill-details.ts
中的billItem.BillNo
一开始是没有值的,只有当我点击bill-list.html
中的账单号按钮时才会定义。我如何最初定义 billItem,然后在单击账单编号按钮时替换。
视图在设置 billItem
之前加载。
您可以使用安全导航运算符 ?
。
<ion-input text-right type="text" [value]="billItem?.BillNo" readonly></ion-input> //billItem model has BillNo property
或者在bill-details.ts的构造函数中设置为空对象:
constructor(...){
if(! this.billItem){
this.billItem={}
}
}
我正在使用 Ionic 2,其中一个组件有两个组件,并且使用发射器共享数据。但是当我执行程序时,就出现了这个错误。
Runtime Error Uncaught (in promise): TypeError: Cannot read property 'BillNo' of undefined TypeError: Cannot read property 'BillNo' of undefined at Object.eval [as updateDirectives]
这是我的代码:
bill-settlement.html
...
<page-bill-list (BillSelected)="onBillSelected($event)"></page-bill-list>
...
<page-bill-details [billItem]="billItem"></page-bill-details>
...
账单-settlement.ts
@Component({
selector: 'page-bill-settlement',
templateUrl: 'bill-settlement.html',
})
export class BillSettlement {
...
billItem: BillDetail
...
onBillSelected(billData: BillDetail) {
this.billItem = billData
}
}
bill-list.html
<ion-buttons>
<button ion-button *ngFor="let item of billItems" (click)="getBillDetails(item)">
{{item.BillNo}}
</button>
</ion-buttons>
bill-list.ts
@Component({
selector: 'page-bill-list',
templateUrl: 'bill-list.html',
})
export class BillList {
billItems: BillDetail[] = []
billItem = new BillDetail()
@Output() BillSelected = new EventEmitter<BillDetail>()
constructor(public navCtrl: NavController,
public navParams: NavParams,
public billSrv: BillerService,
public authSrv: AuthService,
public genSrv: GenericService) {
this.billSrv.getBills()
.subscribe(data => {
this.billItems = data
})
}
getBillDetails(item: BillDetail) {
this.BillSelected.emit(this.billItem)
}
}
bill-details.ts
@Component({
selector: 'page-bill-details',
templateUrl: 'bill-details.html',
})
export class BillDetails {
...
@Input() billItem: BillDetail
...
}
bill-details.html
...
<ion-input text-right type="text" [value]="billItem.BillNo" readonly></ion-input> //billItem model has BillNo property
...
问题是bill-details.ts
中的billItem.BillNo
一开始是没有值的,只有当我点击bill-list.html
中的账单号按钮时才会定义。我如何最初定义 billItem,然后在单击账单编号按钮时替换。
视图在设置 billItem
之前加载。
您可以使用安全导航运算符 ?
。
<ion-input text-right type="text" [value]="billItem?.BillNo" readonly></ion-input> //billItem model has BillNo property
或者在bill-details.ts的构造函数中设置为空对象:
constructor(...){
if(! this.billItem){
this.billItem={}
}
}