【已解决】报错:ERROR: Failed :entry:default@PreviewArkTS... > hvigor ERROR: ArkTS:ERROR File: E:/xxx Indexed access is not supported for fields (arkts-no-props-by-index)

尚硅谷鸿蒙开发课程:p057传递数据
应该由于老师用的SDk是版本9,我使用的SDk版本是12,官方更新过开发规范
参考链接:https://segmentfault.com/q/1010000044999395
报错预览

修改:
1.在EndPage.ets中,新定义一个描述params的结构接口
2.修改 params['name'] 为 params.name,不使用索引访问,更符合 ArkTS 语法。

  import router from '@ohos.router'

  // 定义接口,描述params的结构
  interface Params {
    id: number;
    name: string;
    age: string;
  }

  @Entry
  @Component
  struct EndPage {
    build() {
      Column({ space: 50 }) {
        Text('EndPage')
          .fontSize(40)
          .fontWeight(FontWeight.Bold)
        Blank()
        Button('打印参数')
          .onClick(() => {
            let params = router.getParams() as Params;
            console.log(`name: ${params.name}`)
            // console.log(`name:${params['name']}`)
          })
      }.width('100%')
      .height('100%')
      .justifyContent(FlexAlign.Center)
      .padding({ top: 200, bottom: 200 })
    }
  }

结果:

posted @ 2024-12-02 15:41  zi_ziju  阅读(1557)  评论(0)    收藏  举报