我是靠谱客的博主 殷勤裙子,这篇文章主要介绍[TypeScript 报错] Cannot set property ‘XXXXXXXX‘ of undefined,现在分享给大家,希望可以做个参考。

这个问题是给ts 对象赋值的时候出现的

先说明结果: 需要给声明对象初始化

错误由来->
对象

export interface OrderInterface{
  orderId?: string;
  userId?: string;
  userName?:string;
  carModelName?: string;
  orderTime?: string;
  orderState?: string;
  model?: string;
  outwardWheel?: string;
  outwardColor?:string;
  interior?: string;
  features?: string;
  carImg?:string;
  carPrice?:string;
}

某.ts文件

...省略...
  order:OrderInterface;
...省略....
  this.order.carPrice = this.inputCarPrice;
  this.order.carImg = this.Photo;
...省略....

报错

Cannot set property ‘display’ of undefined

这个时候问题出在 ts 中没有无参构造 所以需要在声明这个"对象"的时候进行初始化

某.ts文件

...省略...
   order:OrderInterface={
    orderId:"",
    userId: "",
    userName: "",
    carModelName: "",
    orderTime: "",
    orderState: "",
    model: "",
    outwardWheel: "",
    outwardColor: "",
    interior: "",
    features: "",
    carImg: "",
    carPrice: "",
  };
...省略....
  this.order.carPrice = this.inputCarPrice;
  this.order.carImg = this.Photo;
...省略....

最后

以上就是殷勤裙子最近收集整理的关于[TypeScript 报错] Cannot set property ‘XXXXXXXX‘ of undefined的全部内容,更多相关[TypeScript内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(111)

评论列表共有 0 条评论

立即
投稿
返回
顶部