• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 通用属性
2<!--Kit: ArkUI-->
3<!--Subsystem: ArkUI-->
4<!--Owner: @CCFFWW-->
5<!--Designer: @yangfan229-->
6<!--Tester: @lxl007-->
7<!--Adviser: @HelloCrease-->
8
9>  **说明:**
10>  从API version 4开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
11
12## 常规属性
13
14常规属性是指组件普遍支持的用来设置组件基本标识和外观显示特征的属性。
15
16| 名称                        | 类型      | 默认值   | 必填   | 描述                                       |
17| ------------------------- | ------- | ----- | ---- | ---------------------------------------- |
18| id                        | string  | -     | 否    | 组件的唯一标识。                                 |
19| style                     | string  | -     | 否    | 组件的样式声明。                                 |
20| class                     | string  | -     | 否    | 组件的样式类,用于引用样式表。                          |
21| ref                       | string  | -     | 否    | 用来指定指向子元素或子组件的引用信息,该引用将注册到父组件的$refs&nbsp;属性对象上。 |
22| disabled                  | boolean | false | 否    | 当前组件是否被禁用,在禁用场景下,组件将无法响应用户交互。设置为true时,组件不响应交互事件。设置为false时,组件响应交互事件。            |
23| data                      | string  | -     | 否    | 给当前组件设置data属性,进行相应的数据存储和读取。JS文件中:<br/>-&nbsp;在事件回调中使用&nbsp;e.target.attr.data&nbsp;读取数据,e为事件回调函数入参。<br/>-&nbsp;使用`$element`或者`$refs`获取DOM元素后,通过attr.data&nbsp;进行访问。<br/>从API&nbsp;Version&nbsp;6&nbsp;开始,建议使用data-\*。 |
24| data-\*<sup>6+</sup>      | string  | -     | 否    | 给当前组件设置data-\*属性,进行相应的数据存储和读取。大小写不敏感,如data-A和data-a默认相同。JS文件中:<br/>-&nbsp;在事件回调中使用&nbsp;e.target.dataSet.a读取数据,e为事件回调函数入参。<br/>-&nbsp;使用`$element`或者`$refs`获取DOM元素后,通过dataSet.a进行访问。 |
25| click-effect<sup>5+</sup> | string  | -     | 否    | 通过这个属性可以设置组件的弹性点击效果,当前支持如下三种效果:<br/>-&nbsp;spring-small:建议小面积组件设置,缩放(90%)。<br/>-&nbsp;spring-medium:建议中面积组件设置,缩放(95%)。<br/>-&nbsp;spring-large:建议大面积组件设置,缩放(95%)。 |
26| dir<sup>6+</sup>          | string  | auto  | 否    | 设置元素布局模式,支持设置rtl、ltr和auto三种属性值:<br/>-&nbsp;rtl:使用从右往左布局模式。<br/>-&nbsp;ltr:使用从左往右布局模式。<br/>-&nbsp;auto:跟随系统语言环境。 |
27
28
29## 渲染属性
30
31渲染属性是指组件普遍支持的用来设置组件是否渲染的属性。
32
33| 名称   | 类型      | 默认值  | 描述                       |
34| ---- | ------- | ---- | ------------------------ |
35| for  | Array   | -    | 根据设置的数据列表,展开当前元素。        |
36| if   | boolean | -    | 根据设置的boolean值,添加或移除当前元素。true表示添加当前元素,false表示移除当前元素。 |
37| show | boolean | -    | 根据设置的boolean值,显示或隐藏当前元素。true表示显示当前元素,false表示隐藏当前元素。 |
38
39>  **说明:**
40>  属性和样式不能混用,不能在属性字段中进行样式设置。
41
42## 示例
43
44### 示例1
45
46```html
47<!-- xxx.hml -->
48<div id="container">
49    <button class="btn" type="capsule" value="toggleDisplay" onclick="toggleDisplay"></button>
50	<list class="list">
51		<list-item for="{{ array }}" class="listItem">
52			<text class="text" onclick="toggleShow" show="{{ visible }}"
53                  if="{{ display }}">{{ $item.value }}</text>
54		</list-item>
55	</list>
56</div>
57```
58
59```css
60/* xxx.css */
61#container {
62    flex-direction: column;
63    width: 100%;
64    margin-top: 10px;
65}
66.text {
67    font-size: 50px;
68    font-weight: 500;
69    margin-left: 12px;
70}
71.listItem {
72    width: 100%;
73    height: 100px;
74    line-height: 60px;
75    border-bottom: 1px solid #DEDEDE;
76    font-size: 20px;
77}
78.btn{
79    width: 280px;
80    font-size: 26px;
81    margin: 10px 0;
82}
83```
84
85```js
86// xxx.js
87export default {
88	data: {
89        visible: true,
90        display: true,
91        title: "",
92        i: 4,
93        array: [
94            {"value": "列表文本0"},
95            {"value": "列表文本1"},
96            {"value": "列表文本2"},
97            {"value": "列表文本3"},
98        ],
99    },
100    toggleShow: function() {
101        this.array.push({"value": "列表文本" + this.i })
102        this.i++
103    },
104    toggleDisplay: function() {
105        this.display = !this.display
106    },
107}
108```
109
110![zh-cn-attributes](figures/zh-cn-attributes.gif)
111
112### 示例2
113
114```html
115<!-- xxx.hml -->
116<div class="container">
117    <div>
118        <text class="text1" dir='rtl' >hello world</text>
119    </div>
120    <div>
121        <text class="text2" dir='ltr' >hello world</text>
122    </div>
123</div>
124```
125
126```css
127/* xxx.css */
128.container {
129    display: flex;
130    flex-direction: column;
131    justify-content: center;
132    align-items: center;
133    width: 100%;
134    height: 100%;
135}
136.text1{
137    width: 90%;
138    height: 100px;
139    background-color: aqua;
140}
141.text2{
142    width: 90%;
143    height: 100px;
144    background-color: blue;
145}
146```
147
148![zh-cn_image1](figures/zh-cn_image1.png)