js-components-custom-events.md 1.1 KB
Newer Older
Z
zengyawen 已提交
1
# 自定义事件
M
mamingshuai 已提交
2 3 4

子组件comp定义如下:

Z
zengyawen 已提交
5

M
mamingshuai 已提交
6 7 8 9 10 11 12 13
```
<!-- comp.hml -->
<div class="item">  
   <text class="text-style" onclick="childClicked">点击这里查看隐藏文本</text> 
   <text class="text-style" if="{{showObj}}">hello world</text> 
</div>
```

Z
zengyawen 已提交
14

M
mamingshuai 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
```
/* comp.css */
.item {  
  width: 700px;   
  flex-direction: column;   
  height: 300px;   
  align-items: center;   
  margin-top: 100px;  
} 
.text-style { 
  font-weight: 500; 
  font-family: Courier; 
  font-size: 40px;
}
```

Z
zengyawen 已提交
31

M
mamingshuai 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44
```
// comp.js
export default { 
  data: {  
    showObj: false,  
  },  
  childClicked () {  
    this.$emit('eventType1'); 
    this.showObj = !this.showObj;  
  },  
}
```

Z
zengyawen 已提交
45

M
mamingshuai 已提交
46 47
引入子组件的示例如下:

Z
zengyawen 已提交
48

M
mamingshuai 已提交
49 50 51 52 53 54 55 56
```
<!-- xxx.hml --> 
<element name='comp' src='../../common/component/comp.hml'></element>  
<div class="container">  
  <comp @event-type1="textClicked"></comp>  
</div>
```

Z
zengyawen 已提交
57

M
mamingshuai 已提交
58 59 60 61 62 63 64 65 66 67
```
/* xxx.css */
.container {  
  background-color: #f8f8ff;  
  flex: 1;  
  flex-direction: column;  
  align-content: center; 
} 
```

Z
zengyawen 已提交
68

M
mamingshuai 已提交
69 70 71 72 73 74
```
// xxx.js
export default {    
  textClicked () {},  
}
```