当前位置: 首页 > 知识库问答 >
问题:

typescript - ts 总是提示空object没有XX属性,要怎么改?

查锦程
2024-03-04




红色波浪线总是提示空的{}没有XX属性,这是从vue2的方法搬过来的,vue2是可以的,vue3要怎么改

export interface PlaneData {  id?: number;  code?: string;  model?: string;  label?: string;  airlines?: string;  type_id?: number;  type_value?: string;  property_id?: number;  property_value?: string;  first_seats?: number;  biz_seats?: number;  economy_seats?: number;  first_map?: object | undefined;  biz_map?: object | undefined;  economy_map?: object | undefined;  first_fare?: number;  biz_fare?: number;  economy_fare?: number;  sort?: number;  memo?: string;  enabled?: number;  create_by?: string;  update_by?: string;  short_name?: string;}const RowCol = (type: string, data: Object | RowColParam | undefined) => {  mapType.value = type;  isVisible.value = true;  if (formData.value.economy_map.rows && formData.value.economy_map.start_row) {    let rowNumber = ref<string[]>([]);    let last =      Number(formData.value.economy_map.rows) +      Number(formData.value.economy_map.start_row);    for (let i = formData.value.economy_map.start_row; i < last; i++) {      rowNumber.value.push(i);    }    localStorage.setItem("economy", JSON.stringify(rowNumber));  }  if (data) {    ranks.value.row = data.rows;    ranks.value.col = data.columns;    ranks.value.start_row = data.start_row;    ranks.value.columns_name = data.columns_name;    ranks.value.tableData = data.seats;  }};

共有3个答案

殳毅
2024-03-04
export interface PlaneData {  ...  economy_map?: RowColParam;  ...}

看你的代码,那几个属性貌似是 RowColParam 中定义的,那只需要把 economy_map 声明为这个类型就可以。另外,用了 ? 表示可选,就不需要加 | undefined,自带。

魏勇军
2024-03-04

economy_map: object => economy_map: Record<string, any>

何兴安
2024-03-04

加个if判空

这句加了判空,你试试
if (formData?.value?.economy_map?.rows && formData?.value?.economy_map?.start_row)

const RowCol = (type: string, data: Object | RowColParam | undefined) => {  mapType.value = type;  isVisible.value = true;  if (formData?.value?.economy_map?.rows && formData?.value?.economy_map?.start_row) {    let rowNumber = ref<string[]>([]);    let last =      Number(formData.value.economy_map.rows) +      Number(formData.value.economy_map.start_row);    for (let i = formData.value.economy_map.start_row; i < last; i++) {      rowNumber.value.push(i);    }    localStorage.setItem("economy", JSON.stringify(rowNumber));  }  if (data) {    ranks.value.row = data.rows;    ranks.value.col = data.columns;    ranks.value.start_row = data.start_row;    ranks.value.columns_name = data.columns_name;    ranks.value.tableData = data.seats;  }};

参考这里:
TS报错:类型‘object‘上不存在属性‘product_id’

 类似资料: