ts-component-based-preview.md 1.4 KB
Newer Older
Z
zengyawen 已提交
1
# @Preview<a name="ZH-CN_TOPIC_0000001124516048"></a>
Z
zengyawen 已提交
2

Z
zengyawen 已提交
3
**@Preview**装饰的自定义组件可以在DevEco Studio的预览器上进行预览,加载页面时,将创建并呈现**@Preview**装饰的自定义组件。
Z
zengyawen 已提交
4

5
>![icon-note.gif](public_sys-resources/icon-note.gif) **说明:** 
Z
zengyawen 已提交
6
>在单个源文件中,最多可以使用**@Preview**装饰一个自定义组件。
Z
zengyawen 已提交
7

Z
zengyawen 已提交
8
## 示例<a name="section2270154810523"></a>
Z
zengyawen 已提交
9 10 11 12 13 14 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

**@Preview**的用法如下:

```
// Display only Hello Component1 on the PC preview. The content under MyComponent is displayed on the real device.
@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)
            }
        }
    }
}
```
Z
zengyawen 已提交
60