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


E
ester.zhou 已提交
4
You can use @Preview to decorate a custom component so that it can be previewed in DevEco Studio. This component is created and displayed when the page where it is located is loaded.
Z
zengyawen 已提交
5 6


E
ester.zhou 已提交
7
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
G
ge-yafang 已提交
8 9 10 11
> In a source file, at most one custom component can be decorated by @Preview.


Example of using @Preview:
Z
zengyawen 已提交
12 13

```
H
HelloCrease 已提交
14
// Display only Hello Component1 on the preview. The content under MyComponent is displayed on the real device.
Z
zengyawen 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
@Entry
@Component
struct MyComponent {
    build() {
        Column() {
            Row() {
                Text('Hello World!')
                    .fontSize("50lpx")
                    .fontWeight(FontWeight.Bold)
            }
            Row() {
                Component1()
            }
            Row() {
                Component2()
            }
        }
    }
}
@Preview
@Component
struct Component1 {
    build() {
        Column() {
            Row() {
                Text('Hello Component1')
                    .fontSize("50lpx")
                    .fontWeight(FontWeight.Bold)
            }
        }
    }
}

@Component
struct Component2 {
    build() {
        Column() {
            Row() {
                Text('Hello Component2')
                    .fontSize("50lpx")
                    .fontWeight(FontWeight.Bold)
            }
        }
    }
}
```