ts-component-based-preview.md 1.2 KB
Newer Older
G
ge-yafang 已提交
1
# @Preview
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3
You can use **@Preview** to decorate a custom component so that it can be previewed in real time in DevEco Studio. This component is created and displayed when the page where it is located is loaded. Dynamic pictures and dynamic preview are not yet supported.
Z
zengyawen 已提交
4 5


E
ester.zhou 已提交
6 7
> **NOTE**
>
E
ester.zhou 已提交
8
> In a source file, at most 10 custom components can be decorated by **@Preview**. For details, see [@Preview](https://developer.harmonyos.com/en/docs/documentation/doc-guides/ohos-previewing-app-service-0000001218760596#section146052489820).
G
ge-yafang 已提交
9 10


E
ester.zhou 已提交
11
Example of using **@Preview**:
Z
zengyawen 已提交
12

E
ester.zhou 已提交
13

E
ester.zhou 已提交
14 15
```ts
// xxx.ets
Z
zengyawen 已提交
16 17 18
@Entry
@Component
struct MyComponent {
E
ester.zhou 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31
  build() {
    Column() {
      Row() {
        Text('Hello World!')
          .fontSize("50lpx")
          .fontWeight(FontWeight.Bold)
      }
      Row() {
        Component1()
      }
      Row() {
        Component2()
      }
Z
zengyawen 已提交
32
    }
E
ester.zhou 已提交
33
  }
Z
zengyawen 已提交
34
}
E
ester.zhou 已提交
35

Z
zengyawen 已提交
36 37 38
@Preview
@Component
struct Component1 {
E
ester.zhou 已提交
39 40 41 42 43 44 45
  build() {
    Column() {
      Row() {
        Text('Hello Component1')
          .fontSize("50lpx")
          .fontWeight(FontWeight.Bold)
      }
Z
zengyawen 已提交
46
    }
E
ester.zhou 已提交
47
  }
Z
zengyawen 已提交
48 49 50 51
}

@Component
struct Component2 {
E
ester.zhou 已提交
52 53 54 55 56 57 58
  build() {
    Column() {
      Row() {
        Text('Hello Component2')
          .fontSize("50lpx")
          .fontWeight(FontWeight.Bold)
      }
Z
zengyawen 已提交
59
    }
E
ester.zhou 已提交
60
  }
Z
zengyawen 已提交
61 62
}
```