openharmony 鸿蒙 ts-universal-component-previewer

2026-08-25 浏览 (1)

Component Preview

Component preview enables you to preview the UI effect of individual custom components.

NOTE

The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.

For details about usage, see Component Preview.

@Preview Decorator

The @Preview decorator decorates custom components for preview.

NOTE

This API is supported in ArkTS widgets, though component preview itself is not supported in ArkTS widgets.

Widget capability: This API can be used in ArkTS widgets since API version 9.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.ArkUI.ArkUI.Full

PreviewParams9+

Implements a configuration object for @Preview parameters.

Defines preview device attributes such as device type and screen state.

Widget capability: This API can be used in ArkTS widgets since API version 9.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeRead-OnlyOptionalDescription
titlestringNoYesName of the component module to preview. Default value: custom component name.
widthnumberNoYesWidth of the preview device, in px. Default value: 1080.
heightnumberNoYesHeight of the preview device, in px. Default value: 2340.
localestringNoYesDevice language (for example, zh_CN and en_US). Default value: zh_CN.
colorModestringNoYesColor mode of the preview device. The default value is light for non-TV devices (only light supported) and dark for TV devices (only dark supported).
deviceTypestringNoYesDevice type for preview rendering. Default value: Phone.
dpinumberNoYesDPI of the preview device. Default value: 480.
orientationstringNoYesScreen orientation of the preview device. Options: portrait (default), landscape.
roundScreenbooleanNoYesWhether the preview screen is circular. Default value: false. true: circular. false: non-circular.

Example.

This example demonstrates @Preview usage with and without parameters.

@Entry
@Preview
@Component
struct Index {
  @State message: string = 'default Preview';

  build() {
    RelativeContainer() {
      Text(this.message)
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
    }
    .height('100%')
    .width('100%')
  }
}

@Preview({
  title: 'PreviewParams',
  width: 540,
  height: 1170
})
@Component
struct Test {
  @State message: string = 'PreviewParams';

  build() {
    RelativeContainer() {
      Text(this.message)
        .fontSize(40)
        .fontWeight(FontWeight.Bold)
    }
    .height('100%')
    .width('100%')
  }
}

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 ts-drawing-components-shape

openharmony 鸿蒙 ts-universal-attributes-sharp-clipping

openharmony 鸿蒙 ts-universal-attributes-toolbar

openharmony 鸿蒙 ts-container-lazyvgridlayout

openharmony 鸿蒙 ohos-arkui-advanced-FoldSplitContainer

openharmony 鸿蒙 ts-methods-calendarpicker-dialog

openharmony 鸿蒙 ts-uicommonevent

openharmony 鸿蒙 ts-basic-gestures-tapgesture

openharmony 鸿蒙 ohos-arkui-advanced-DialogV2

openharmony 鸿蒙 ts-components-canvas-canvas

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/UG7zVvl2