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