如下列代码,将组件传进去的键值使用了’'括起,因此才会在改变后,子组件接收到了undefined,未改变时接收的值是正常的。
解决错误将引号去掉即可
同样的,如果是@Link接收,使用引号会直接报错'@Link' decorated 'count' must be initialized through the component constructor. <ArkTSCheck>,编译报错Property 'count' in the custom component 'CountDownComponent' is missing

@Component
struct CountDownComponent {
  @Prop count: number = 0;

  build() {
    Column() {
       Text(`${this.count}`)
    }
  }
}

@Entry
@Component
struct ParentComponent {
  @State countDownStartValue: number = 10;

  build() {
    Column() {
      Text(`Grant ${this.countDownStartValue} nuggets to play.`)
      // 父组件的数据源的修改会同步给子组件
      Button(`+1 - Nuggets in New Game`).onClick(() => {
        this.countDownStartValue += 1;
      })
      CountDownComponent({ 'count': this.countDownStartValue, costOfOneAttempt: 2 })
    }
  }
}

错误关键

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐