HarmonyOS鸿蒙Next中selectedCityData数组Object is possibly 'undefined'. <ArkTSCheck>

HarmonyOS鸿蒙Next中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](/user/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’. <ArkTSCheck>

尝试使用as CityModel[],也没有效果 this.selectedCityData = this.params.selectedCityData as CityModel[];

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


步骤二 CYMyProfileImpInformationStuTeacherPage 页面, 获取值  
[@State](/user/State) params: CityModelAllInfomation = router.getParams()  

this.params.selectedCityData.length  

问题:  
selectedCityData 数组  
Object is possibly 'undefined'. <ArkTSCheck>  

尝试使用as CityModel[],也没有效果  
this.selectedCityData = this.params.selectedCityData as CityModel[];  

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

更多关于HarmonyOS鸿蒙Next中selectedCityData数组Object is possibly 'undefined'. <ArkTSCheck>的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

这边用以下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%')
  }
}

更多关于HarmonyOS鸿蒙Next中selectedCityData数组Object is possibly 'undefined'. <ArkTSCheck>的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next开发中,selectedCityData数组出现Object is possibly 'undefined'的警告,表明在TypeScript的类型检查中,selectedCityData可能未被正确定义或初始化。为了消除此警告,需要确保selectedCityData在使用前已经被正确赋值或进行类型断言。

  1. 确保初始化:在使用selectedCityData之前,确保它已经被正确初始化。例如:

    let selectedCityData: CityData[] = [];
    
  2. 类型断言:如果确定selectedCityData在使用时不会是undefined,可以使用类型断言来消除警告。例如:

    let selectedCityData = someFunctionThatReturnsCityData() as CityData[];
    
  3. 可选链操作符:如果selectedCityData可能为undefined,可以使用可选链操作符来安全地访问其属性。例如:

    let city = selectedCityData?.[0];
    
  4. 非空断言:如果确定selectedCityData不会为undefined,可以使用非空断言操作符!。例如:

    let city = selectedCityData![0];
    

通过这些方法,可以有效解决Object is possibly 'undefined'的警告。

在HarmonyOS鸿蒙Next开发中,selectedCityData数组可能为undefined的警告通常是由于TypeScript的类型检查机制检测到该变量可能未初始化或未被赋值。你可以通过以下方式解决:

  1. 类型断言:明确告诉编译器该变量不会为undefined

    const cityData = selectedCityData!;
    
  2. 默认值:为变量提供一个默认值,确保其不为undefined

    const selectedCityData: CityData[] = [];
    
  3. 条件判断:在使用前检查变量是否已定义。

    if (selectedCityData) {
        // 使用selectedCityData
    }
    

选择适合你场景的解决方案即可消除警告。

回到顶部