目录

Previewing an ArkUI Page or Component

ArkUI preview supports page preview, component preview, multi-breakpoint preview, and widget preview. As shown in the figure below, the left icon corresponds to page preview, the middle icon corresponds to component preview, and the right icon corresponds to multi-breakpoint preview. Widget preview can be enabled directly after a widget file is created.

Previewing a Page

ArkTS apps/atomic services support page preview. To use this feature, add the @Entry decorator at the top of the project's .ets file.

The following exemplifies how to use the @Entry decorator:

@Entry
@Component
struct Index {
  @State message: string = 'Hello World'

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

Previewing a Component

ArkTS apps/atomic services support component preview. Component preview supports real-time preview, but not dynamic pictures or dynamic preview. Before previewing a component, you must decorate it with the @Preview decorator. In a single source file, you can use up to 10 @Preview decorators to decorate custom components.

The following exemplifies how to decorate a component with the @Preview decorator:
@Preview({
  title: 'ContentTable'
})
@Component
struct ContentTablePreview {
  build() {
    Flex() {
      ContentTable({ foodItem: getDefaultFoodData() })
    }
  }
}

Below is a preview of the component.

The preset device for preview is a phone in the following settings. To preview your app/atomic service on a different device, modify the fields under @Preview.
@Preview({
  title: 'Component1',  // Name of the component to preview.
  deviceType: 'phone',  // Type of the device for rendering and previewing the component. The default value is phone.
  width: 1080,  // Width of the preview device, in pixels.
  height: 2340, // Height of the preview device, in px.
  colorMode: 'light',  // Color mode of the preview device. The value can only be light.
  dpi: 480, // Screen DPI value of the preview device.
  locale: 'zh_CN', // Language of the preview device, for example, zh_CN or en_US.
  orientation: 'portrait',  // Screen orientation of the preview device. The value can be portrait or landscape.
  roundScreen: false  // Whether the screen shape of the preview device is round.
})

If the component you want to preview depends on parameter injection, add the @Preview decorator as follows: Define a component fragment, declare in the fragment the component to be previewed and parameter dependency, and add the @Preview decorator to the component fragment. For example, to preview the component below:

@Component
struct Title {
  @Prop context: string; 
  build() {
    Text(this.context)
  }
}

Add @Preview as follows:

@Preview
@Component    // Define the component fragment TitlePreview.
struct TitlePreview {
  build() {
    Title({ context: 'MyTitle' })    // Declare the component to preview, Title in this case, and the parameter {context: 'MyTitle'} on which the component depends.
  }
}

Multi-Breakpoint Preview

Since version 26.0.0, multi-breakpoint preview is available for ArkTS apps and atomic services, enabling simultaneous display of preview views for eight typical breakpoints. Real-time preview and quick preview are not supported in multi-breakpoint preview mode. If images are excessively large or numerous, they may fail to be displayed during preview.

Multi-breakpoint preview is implemented by adding the @Entry decorator at the top of the .ets file of your project. The sample code is as follows:

@Entry
@Component
struct Index {
  @State message: string = 'Hello World'

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

The multi-breakpoint preview effect of the preceding sample is shown in the figure below, which displays preview results under eight typical breakpoints.

You can click on each preview view to check the component tree for that breakpoint.

Three-way linkage is supported between the code editor, UI, and component tree:
  • When a component on the UI is selected, the corresponding component in the component tree is selected, and the matching code block in the layout file within the code editor is highlighted.
  • When a code block in the layout file is selected, the corresponding component is highlighted on the UI, and the matching node in the component tree is marked as selected.
  • When a component in the component tree is selected, both the corresponding code block and the UI component are highlighted.
  • Modification of component attributes on the attribute panel is not supported.

Previewing a Widget

After creating a widget and selecting the widget file, click Previewer on the right to preview the effect.