无法读取未定义的“xxx"的属性 [英] Cannot read property of 'xxx' of undefined

查看:25
本文介绍了无法读取未定义的“xxx"的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Ionic 2,其中一个组件有两个组件,并且使用发射器共享数据.但是当我执行程序时,就出现了这个错误.

I'm using Ionic 2, in which a component has two components and the data were shared using emitters. But when I execute the program, it comes to this error.

运行时错误未捕获(承诺):类型错误:无法读取属性未定义类型错误的BillNo":无法读取属性BillNo"在 Object.eval [as updateDirectives] 处未定义

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>
...

bill-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,然后在单击账单编号按钮时进行替换.

The problem is billItem.BillNo in bill-details.ts has no value initially, it is defined only when I click the bill number button in the bill-list.html. How can i define the billItem initially and then replaces when clicked with the bill number button.

推荐答案

在设置 billItem 之前加载视图.

The view is loaded before billItem is set.

您可以使用安全导航操作符?.

You could use a safe navigation operator ?.

<ion-input text-right type="text" [value]="billItem?.BillNo" readonly></ion-input> //billItem model has BillNo property

或者在bill-details.ts的构造函数中设置为空对象:

or set it to empty object in the constructor of bill-details.ts:

constructor(...){
  if(! this.billItem){
    this.billItem={}
  }
}

这篇关于无法读取未定义的“xxx"的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆