HarmonyOS 鸿蒙Next:Assigning the 'REGULAR' decorated attribute 'i' to the '@Prop' decorated attribute 'index' is not allowed

HarmonyOS 鸿蒙Next:Assigning the ‘REGULAR’ decorated attribute ‘i’ to the ‘@Prop’ decorated attribute ‘index’ is not allowed

编译通过可以预览,但是eslint提示错误信息Assigning the ‘REGULAR’ decorated attribute ‘i’ to the ‘@Prop’ decorated attribute ‘index’ is not allowed.

请问如何配置或者解决这种编辑器的错误提示问题


更多关于HarmonyOS 鸿蒙Next:Assigning the 'REGULAR' decorated attribute 'i' to the '@Prop' decorated attribute 'index' is not allowed的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

cke_180.png

更多关于HarmonyOS 鸿蒙Next:Assigning the 'REGULAR' decorated attribute 'i' to the '@Prop' decorated attribute 'index' is not allowed的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS中,@Prop装饰器用于声明一个与父组件状态绑定的属性。@Prop属性通常用于接收父组件传递的数据,并且这些数据是只读的,不能在子组件中直接修改。REGULAR装饰器(即未使用任何装饰器的普通属性)则是一个普通的类属性,可以在组件内部自由修改。

在你的问题中,尝试将一个普通的属性i赋值给@Prop装饰的属性index是不允许的。这是因为@Prop属性是单向绑定的,只能从父组件接收数据,而不能在子组件中被直接赋值或修改。这种设计是为了保证数据流的清晰性和可预测性,避免在子组件中意外修改父组件的状态。

具体来说,@Prop属性的值是由父组件传递的,子组件不能直接对其进行赋值操作。如果你需要在子组件中使用一个可修改的值,可以考虑使用@State@Link装饰器,而不是@Prop

总结:在HarmonyOS中,@Prop装饰的属性是只读的,不能通过赋值操作来修改其值。因此,将REGULAR装饰的属性i赋值给@Prop装饰的属性index是不允许的。

回到顶部