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
相关问题
HarmonyOS Objectobject的区别
545浏览 • 1回复 待解决
HarmonyOS [object, object]是如何读取的?
174浏览 • 1回复 待解决
HarmonyOS object.assign实现
291浏览 • 1回复 待解决
HarmonyOS ArkTS如何判断Object类型?
230浏览 • 1回复 待解决
HarmonyOS Object怎么转换成map
35浏览 • 1回复 待解决
HarmonyOS Object.assign(target, source)报错
1037浏览 • 2回复 待解决
HarmonyOS创建前置广角相机返回undefined
442浏览 • 1回复 待解决
HarmonyOS string object大小写区别
544浏览 • 1回复 待解决
HarmonyOS Object不支持 ... 展开符吗?
264浏览 • 1回复 待解决
HarmonyOS api 11 Object.assign 会报红
198浏览 • 1回复 待解决
HarmonyOS判断Object中是否含有某个属性
976浏览 • 1回复 待解决
HarmonyOS 如何遍历一个JSON Object
372浏览 • 1回复 待解决
使用fetch报错,fetch undefined
7172浏览 • 2回复 待解决
readonly修饰的数组无法获取数组元素
2049浏览 • 1回复 待解决
Object标准库接口限制
999浏览 • 1回复 待解决
提问
该提问已有0人参与 ,帮助了0人