目录

界面预览常见问题

预览告警“@Consume/@Link decorated property <propertyName> not initialized”

问题现象

启动预览后,预览窗口出现如下白屏,并在上方出现“Preview failed. View details in the PreviewerLog window.”的错误信息。

此时下方PreviewerLog窗口出现如下告警信息“@Consume/@Link decorated property <propertyName> not initialized.”

可能原因

如果预览页面/组件内含有@Consume或@Link装饰的成员,预览时可能会出现白屏。

解决措施

不要直接预览含有@Consume或@Link装饰成员的子组件,建议通过预览父组件的形式来查看子组件的预览效果。

示例代码:

// 建议通过在ParentComp上添加@Preview,预览ParentComp来查看ChildComp的预览效果
@Preview // 组件预览
@Component
struct ParentComp {
  // @Provide装饰的变量reviewVotes由入口组件ParentComp提供其后代组件
  @Provide reviewVotes: number = 10;

  build() {
    Column() {
      Button(`reviewVotes(${this.reviewVotes}), give +1`)
        .onClick(() => this.reviewVotes += 1)
      ChildComp()
    }
  }
}

// @Preview不建议直接预览ChildComp
@Component
struct ChildComp {
  // @Consume装饰的变量通过相同的属性名绑定其祖先组件ParentComp内的@Provide装饰的变量
  @Consume reviewVotes: number;
  build() {
    Column() {
      Text(`reviewVotes(${this.reviewVotes})`)
      Button(`reviewVotes(${this.reviewVotes}), give +1`)
        .onClick(() => this.reviewVotes += 1)
    }
    .width('50%')
  }
}

预览告警“There are properties not initialized”

问题现象

启动预览后,预览窗口出现如下白屏,并在上方出现“Preview failed. View details in the PreviewerLog window.”的错误信息。

此时下方PreviewerLog窗口出现如下告警信息“There are properties not initialized.”。

可能原因

预览页面/组件含有未初始化成功的成员变量,调用这些成员变量的属性/方法时会报错,预览白屏。可能导致该问题的典型原因有:

解决措施

请使用自定义Mock。