DevEco Studio将deveco3.0版本的项目运行到HarmonyOS鸿蒙Next5.0,出现报错这个怎么改

DevEco Studio将deveco3.0版本的项目运行到HarmonyOS鸿蒙Next5.0,出现报错这个怎么改

报错主要是这两个类型

@State title: string = router.getParams()['title']
@State author: string = router.getParams()['author']
@State content: string = router.getParams()['content']
@State entirePoem: string = ''
const poemList = res.data['Data']
console.log('Data:' + JSON.stringify(poemList))

poemList.forEach(poem => {
  const pt = poem['Title']
  const pa = poem['Author']
  const pc = poem['Content']

Indexed access is not supported for fields (arkts-no-props-by-index) <ArkTSCheck>

Use explicit types instead of “any”, “unknown” (arkts-no-any-unknown) <ArkTSCheck>

async routePage() {
  let options = {
    url: 'pages/ShowDetails',
    params: {
      title: this.poemTitle,
      author: this.author,
      content: this.content
    }
  }

报错类型Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals) <ArkTSCheck>

Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals) <ArkTSCheck>


更多关于DevEco Studio将deveco3.0版本的项目运行到HarmonyOS鸿蒙Next5.0,出现报错这个怎么改的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

鸿蒙Next 5.0与Deveco 3.0存在API兼容性问题。检查项目中的SDK版本是否支持Next 5.0,修改build.gradle文件中的compileSdkVersion和targetSdkVersion为Next 5.0对应的版本号。删除旧的依赖库,替换为Next 5.0官方文档中列出的兼容库版本。若使用方舟编译器特有语法,需改为Next 5.0支持的ArkTS标准语法。同步项目配置后清理重建。

更多关于DevEco Studio将deveco3.0版本的项目运行到HarmonyOS鸿蒙Next5.0,出现报错这个怎么改的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


从报错信息来看,主要是HarmonyOS Next 5.0对ArkTS的类型检查更加严格导致的几个问题:

  1. 索引访问问题(arkts-no-props-by-index): 需要将router.getParams()['title']这种索引访问方式改为类型安全的方式:
const params = router.getParams() as {title: string, author: string, content: string}
@State title: string = params.title
  1. 禁止any/unknown类型(arkts-no-any-unknown): 需要为poemListpoem对象定义明确的类型:
interface Poem {
  Title: string;
  Author: string;
  Content: string;
}

const poemList = res.data['Data'] as Poem[]
  1. 对象字面量类型问题(arkts-no-untyped-obj-literals): 需要为options参数定义类型:
interface RouterOptions {
  url: string;
  params: {
    title: string;
    author: string;
    content: string;
  }
}

let options: RouterOptions = {
  url: 'pages/ShowDetails',
  params: {
    title: this.poemTitle,
    author: this.author,
    content: this.content
  }
}

这些修改都是为了符合ArkTS在HarmonyOS Next 5.0中更严格的类型检查要求。

回到顶部