HarmonyOS鸿蒙Next中Indexed access is not supported for fields (arkts-no-props-by-index) <ArkTSCheck>
HarmonyOS鸿蒙Next中Indexed access is not supported for fields (arkts-no-props-by-index) <ArkTSCheck>
报错:Indexed access is not supported for fields (arkts-no-props-by-index) <ArkTSCheck>
我的代码
```javascript
import { router } from '[@kit](/user/kit).ArkUI';
[@Entry](/user/Entry)
[@Component](/user/Component)
struct Index {
[@State](/user/State) message: string = '欢迎回来,主人!';
[@State](/user/State) userName: string = '';//用户名
[@State](/user/State) passWord: string = '';//密码
build() {
RelativeContainer() {
Text(this.message)
.id('HelloWorld')
.fontSize(40)
.fontWeight(FontWeight.Bold)
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
})
TextInput({placeholder:'用户名'}).width('90%').margin({top:100})
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
})
.onChange((value)=>{
this.userName = value;
})
TextInput({placeholder:'密码'}).width('90%').margin({top:200}).type(InputType.Password)
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
})
.onChange((value)=>{
this.passWord = value;
})
Button('登录')
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
})
.margin({top:300})
.onClick(()=>{
router.pushUrl({
url:'pages/LoginOKPage',
params:{
userNameParam:this.userName,
passWordParam:this.passWord
}
})
})
}
.height('100%')
.width('100%')
}
}
更多关于HarmonyOS鸿蒙Next中Indexed access is not supported for fields (arkts-no-props-by-index) <ArkTSCheck>的实战教程也可以访问 https://www.itying.com/category-93-b0.html
您好!
请问您是在什么时候报错?做了什么操作吗?
我直接运行了您的代码,随便输入了账号密码,点击登录和隐藏密码的按钮,都没有报错;
更多关于HarmonyOS鸿蒙Next中Indexed access is not supported for fields (arkts-no-props-by-index) <ArkTSCheck>的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,出现“Indexed access is not supported for fields (arkts-no-props-by-index) <ArkTSCheck>”错误提示,是因为在ArkTS(Ark TypeScript)中不支持通过索引访问字段。ArkTS是鸿蒙系统的一种类型安全的编程语言,设计上更注重类型安全和代码的健壮性。
具体来说,ArkTS不允许通过类似obj[index]的方式访问对象的属性,这是因为这种访问方式可能会导致类型不安全或运行时错误。ArkTS鼓励使用明确的属性名来访问对象字段,以确保代码的可读性和类型安全。
例如,以下代码在ArkTS中是不合法的:
let obj = { name: "HarmonyOS", version: "Next" };
let field = obj["name"]; // 错误:Indexed access is not supported for fields
正确的做法是直接使用属性名访问:
let obj = { name: "HarmonyOS", version: "Next" };
let field = obj.name; // 正确
这种设计有助于减少潜在的错误,并提高代码的可维护性。
在HarmonyOS鸿蒙Next中,“Indexed access is not supported for fields (arkts-no-props-by-index)” 是一个静态类型检查错误。它表示你在代码中尝试通过索引访问对象的字段,而ArkTS语言不支持这种访问方式。ArkTS要求通过明确的字段名来访问对象属性,而不是通过数组索引的方式。你应检查代码并确保使用正确的字段名来访问对象属性。

