OpenHarmony开发者论坛

标题: 向子组件传递函数报错 [打印本页]

作者: stevending1st    时间: 2024-11-23 11:34
标题: 向子组件传递函数报错
[md]### 【问题描述】

1. 介绍问题现象和发生的背景
   当向子组件传递一个函数,报错
2. 相关的代码(请勿使用截图)

   ```typescript
   @Entry
   @Component
   struct TestPage {
     submit: () => boolean = () => {
       return true
     }

     build() {
       RelativeContainer() {
         MyRichEditor({
           onTest: this.submit
         })
       }
       .height('100%')
       .width('100%')
     }
   }

   @Component
   struct MyRichEditor {
     @Prop onTest: () => boolean;
     controller: RichEditorController = new RichEditorController();

     build() {
       Column() {
         RichEditor({ controller: this.controller })
           .border({width: "1"})
           .onWillChange(() => {
             if (this.onTest) {
               return this.onTest()
             } else {
               return true
             }
           })
       }
     }
   }
   ```
3. 运行结果、错误截图

   ```plaintext
   Illegal variable value error with decorated variable undefined 'onTest': failed validation: 'undefined, null, number, boolean, string, or Object but not function, not V2 @ObservedV2 / @Trace class, and makeObserved return value either, attempt to assign value type: 'function', value: 'undefined'!
   ```
4. 我尝试过的解决方法和结果
5. 我想要达到的结果

   正常运行

### 【运行环境】

硬件:
ROM版本:
DevEvoStudio版本:
SDK版本:12
[/md]
作者: 马迪    时间: 2024-11-23 19:11
  把  
@Prop onTest: () => boolean;
改成
onTest?: () => boolean;
试试?




欢迎光临 OpenHarmony开发者论坛 (https://forums.openharmony.cn/) Powered by Discuz! X3.5