HarmonyOS selectedCityData 数组 Object is possibly 'undefined'. <ArkTSCheck>

步骤一: CYMyProfileImpInformationStuTeacherCityPage路由跳转到达CYMyProfileImpInformationStuTeacherPage页面。

@Entry  
@Component  
export struct CYMyProfileImpInformationStuTeacherCityPage  
export class  CityModel{  
  provid?: number;  
  name?: string;  
}  
export default class  CityModelAllInfomation{  
  
  type?: string;  
  selectedCityData?: CityModel[];  
}  
 @State selectedCityData: CityModel[] = [];  
  
 let cityModelAllInfomation: CityModelAllInfomation = {  
      selectedCityData: this.selectedCityData,  
    }  
router.back({  
        url: 'pages/CYMyProfileImpInformationStuTeacherPage',  
        params: cityModelAllInfomation   
      })

步骤二 :CYMyProfileImpInformationStuTeacherPage 页面, 获取值。

@State params: CityModelAllInfomation = router.getParams()    
this.params.selectedCityData.length

问题:selectedCityData 数组Object is possibly 'undefined',尝试使用as CityModel[],也没有效果。

this.selectedCityData = this.params.selectedCityData as CityModel[];

想取出数组信息,拼接地区,如北京 北京市 朝阳区。

HarmonyOS
2024-10-09 12:32:56
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get

这边用以下demo复现问题,传CityModel[]这个数组的话,是没问题的:

//CityModel.ets  
export class CityModel{  
  provid?: number;  
  name?: string;  
  constructor(provid:number,name?:string) {  
    this.provid = provid  
    this.name = name  
  }  
}
//CityModelAllInfomation.ets  
import { CityModel } from './CityModel';  
export class CityModelAllInfomation {  
  type?: string;  
  userInformationIdentificationCityStr?: string;  
  title?: string;  
  provid?: number;  
  selectedCityData?: CityModel[];  
}
//CYMyProfileImpInformationStuTeacherPage.ets  
import { router } from '@kit.ArkUI'  
import { CityModel } from './CityModel'  
import { CityModelAllInfomation } from './CityModelAllInfomation'  
@Entry  
@Component  
struct CYMyProfileImpInformationStuTeacherPage {  
  @State CityModel: CityModel[] = router.getParams() as CityModel[]  
  build() {  
    Row() {  
      Column() {  
        Button("按我输出")  
          .fontSize(50)  
          .fontWeight(FontWeight.Bold)  
          .onClick(()=>{  
            console.log("this.CityModel[0].name="+this.CityModel[0].name)  
            console.log("this.CityModel[1].name="+this.CityModel[1].name)  
          })  
        Button('返回')  
          .onClick(()=>{  
            router.back()  
          })  
      }  
      .width('100%')  
    }  
    .height('100%')  
  }  
}
//Index.ets  
import { CityModel } from './CityModel';  
import { router } from '@kit.ArkUI';  
@Entry  
@Component  
struct A {  
  @State message: string = 'Hello World';  
  @State selectedCityData: CityModel[] = [new CityModel(1,'123'),new CityModel(2)]  
  build() {  
    Row() {  
      Column() {  
        Button(this.message)  
          .fontSize(50)  
          .fontWeight(FontWeight.Bold)  
          .onClick(()=>{  
            //这边只传了CityModel[]类型的参数  
            // let cityModelAllInfomation: CityModelAllInfomation = {  
            //   selectedCityData: this.selectedCityData,  
            // }  
            router.pushUrl({  
              url: 'pages/CYMyProfileImpInformationStuTeacherPage',  
              params: this.selectedCityData  
            })  
          })  
      }  
      .width('100%')  
    }  
    .height('100%')  
  }  
}
分享
微博
QQ
微信
回复
2024-10-09 17:36:13
提问
该提问已有0人参与 ,帮助了0人