问题现象
启动预览后,预览窗口出现如下白屏,并在上方出现“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%')
}
} 问题现象
启动预览后,预览窗口出现如下白屏,并在上方出现“Preview failed. View details in the PreviewerLog window.”的错误信息。

此时下方PreviewerLog窗口出现如下告警信息“There are properties not initialized.”。
可能原因
预览页面/组件含有未初始化成功的成员变量,调用这些成员变量的属性/方法时会报错,预览白屏。可能导致该问题的典型原因有:
解决措施
请使用自定义Mock。
import { MockSetup } from '@ohos/hamock';
export default class Fruit{
public name: string;
getName(): string{
return this.name;
}
constructor(name: string) {
this.name = name;
}
}
@Entry
@Component
struct GlobalData {
@State fruit:Fruit = AppStorage.get("fruit") as Fruit;
@MockSetup
mock(){
this.fruit = new Fruit("apple");
}
build() {
Row() {
Column() {
Text(this.fruit.name)
.fontSize(50)
.fontWeight(FontWeight.Bold)
}
.width('100%')
}
.height('100%')
}
} import { MockSetup } from '@ohos/hamock';
import { router } from '@kit.ArkUI';
@Entry
@Component
struct Page {
@State params: object = router.getParams();
@MockSetup
mock(){
this.params = [];
this.params["path"] = "path";
}
build() {
Row() {
Column() {
Text(this.params['path'])
.fontSize(50)
.fontWeight(FontWeight.Bold)
}
.width('100%')
}
.height('100%')
}
}