HarmonyOS鸿蒙Next中不支持属性名不是标识符的Objects (arkts-identifiers-as-prop-names)

HarmonyOS鸿蒙Next中不支持属性名不是标识符的Objects (arkts-identifiers-as-prop-names)

Objects with property names that are not identifiers are not supported (arkts-identifiers-as-prop-names)

代码如下:

```php
const FoodComposition: any[] = [
  { "name": "Tomato", 'image': $r('app.media.Tomato'), 'category': Category.Vegetable, 'calories': 17, 'protein': 0.9, 'fat': 0.2, 'carbohydrates': 3.9, 'vitaminC': 17.8 },
  { 'name': 'Walnut', 'image': $r('app.media.Walnut'), 'category': Category.Nut, 'calories': 654 , 'protein': 15, 'fat': 65, 'carbohydrates': 14, 'vitaminC': 1.3 },
  { 'name': 'Cucumber', 'image': $r('app.media.Cucumber'), 'category': Category.Vegetable, 'calories': 30, 'protein': 3, 'fat': 0, 'carbohydrates': 1.9, 'vitaminC': 2.1 },
  { 'name': 'Blueberry', 'image': $r('app.media.Blueberry'), 'category': Category.Fruit, 'calories': 57, 'protein': 0.7, 'fat': 0.3, 'carbohydrates': 14, 'vitaminC': 9.7 },
  { 'name': 'Crab', 'image': $r('app.media.Crab'), 'category': Category.Seafood, 'calories': 97, 'protein': 19, 'fat': 1.5, 'carbohydrates': 0, 'vitaminC': 7.6 },
  { 'name': 'IceCream', 'image': $r('app.media.IceCream'), 'category': Category.Dessert, 'calories': 207, 'protein': 3.5, 'fat': 11, 'carbohydrates': 24, 'vitaminC': 0.6 },
  { 'name': 'Onion', 'image': $r('app.media.Onion'), 'category': Category.Vegetable, 'calories': 39, 'protein': 1.1, 'fat': 0.1, 'carbohydrates': 9, 'vitaminC': 7.4 },
  { 'name': 'Mushroom', 'image': $r('app.media.Mushroom'), 'category': Category.Vegetable, 'calories': 22, 'protein': 3.1, 'fat': 0.3, 'carbohydrates': 3.3, 'vitaminC': 2.1 },
  { 'name': 'Kiwi', 'image': $r('app.media.Kiwi'), 'category': Category.Fruit, 'calories': 60 , 'protein': 1.1, 'fat': 0.5, 'carbohydrates': 15, 'vitaminC': 20.5 },
  { 'name': 'Pitaya', 'image': $r('app.media.Pitaya'), 'category': Category.Fruit, 'calories': 60, 'protein': 1.2, 'fat': 0, 'carbohydrates': 10, 'vitaminC': 60.9 },
  { 'name': 'Avocado', 'image': $r('app.media.Avocado'), 'category': Category.Fruit, 'calories': 160, 'protein': 2, 'fat': 15, 'carbohydrates': 9, 'vitaminC': 10 },
  { 'name': 'Strawberry', 'image': $r('app.media.Strawberry'), 'category': Category.Fruit, 'calories': 32, 'protein': 0.7, 'fat': 0.3, 'carbohydrates': 8, 'vitaminC': 58.8 }
]

更多关于HarmonyOS鸿蒙Next中不支持属性名不是标识符的Objects (arkts-identifiers-as-prop-names)的实战教程也可以访问 https://www.itying.com/category-93-b0.html

5 回复

更多关于HarmonyOS鸿蒙Next中不支持属性名不是标识符的Objects (arkts-identifiers-as-prop-names)的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


打不开是怎么回事,

应用代码

interface W {
  bundleName: string
  action: string
  entities: string[]
}

let wantInfo: W = {
  'bundleName': 'com.huawei.hmos.browser',
  'action': 'ohos.want.action.viewData',
  'entities': ['entity.system.browsable']
}

建议改法

interface W {
  bundleName: string
  action: string
  entities: string[]
}

let wantInfo: W = {
  bundleName: 'com.huawei.hmos.browser',
  action: 'ohos.want.action.viewData',
  entities: ['entity.system.browsable']
}

在HarmonyOS鸿蒙Next中,ArkTS(Ark TypeScript)对属性名的使用有特定要求。ArkTS不支持属性名不是标识符的Objects。标识符是指符合编程语言命名规则的名称,通常以字母、下划线或美元符号开头,后续可以包含字母、数字、下划线或美元符号。如果属性名不符合这些规则,例如使用数字开头或包含特殊字符,ArkTS会报错提示arkts-identifiers-as-prop-names

例如,以下代码在ArkTS中会报错:

let obj = {
    "123name": "value", // 报错,属性名以数字开头
    "name-age": "value" // 报错,属性名包含特殊字符
};

正确的写法应为:

let obj = {
    name123: "value", // 合法标识符
    nameAge: "value" // 合法标识符
};

ArkTS的设计遵循了JavaScript/TypeScript的标识符规则,以确保代码的一致性和可维护性。如果属性名不符合标识符规则,需要使用其他方式处理,例如使用Map或字符串键值对。

在HarmonyOS鸿蒙Next中,ArkTS要求对象的属性名必须是有效的标识符。这意味着属性名必须以字母、下划线或美元符号开头,且不能包含特殊字符或空格。如果属性名不符合标识符规则,编译器会抛出arkts-identifiers-as-prop-names错误。例如,{ "my-prop": 123 }是不允许的,应改为{ myProp: 123 }

回到顶部