HarmonyOS使用as关键字转换为指定类型,结果为undefined

示例代码:

export class RouterParams{ 
  param : Record<string,object | ValueType> = {} 
} 
export class StockChartPageModel extends RouterParams{ 
  stockCodes: Array<string> = [] 
} 
let params: RouterParams = new RouterParams() 
let stockParams = params as StockChartPageModel
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

结果stockParams值为undefined。

HarmonyOS
2024-08-27 14:24:30
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

可以参照这个demo:

import { ValueType } from '@kit.ArkData'; 
@Entry 
@Component 
struct AsDemo { 
  @State message: string = 'Hello World'; 
  aboutToAppear(): void { 
    let params: RouterParams = new RouterParams() 
    let stockParams = params as StockChartPageModel 
    params.param = {"name":"tom"} 
    console.log('父类集合内容:'+stockParams.param["name"]) 
  } 
  build() { 
    Row() { 
      Column() { 
        Text(this.message) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
} 
export class RouterParams{ 
  param : Record<string,object | ValueType> = {} 
} 
export class StockChartPageModel extends RouterParams{ 
  stockCodes: Array<string> = [] 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.

params属性没有StockChartPageModel这个属性值,但是有他自己本身类的param属性值。

分享
微博
QQ
微信
回复
2024-08-27 19:04:03
相关问题
HarmonyOS let关键字
986浏览 • 1回复 待解决
HarmonyOS 上是否有in关键字
938浏览 • 1回复 待解决
数据库入表时无法使用内建关键字
2750浏览 • 1回复 待解决
HarmonyOS Text搜索关键字高亮功能
1861浏览 • 1回复 待解决
HarmonyOS getParamByName结果类型转换
776浏览 • 1回复 待解决
API15中InteractionHand枚举类型undefined
253浏览 • 0回复 待解决
PixelMap类型怎么转换成Base64符串
1618浏览 • 1回复 待解决
HarmonyOS Length 如何转换为具体数值?
1159浏览 • 1回复 待解决