• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 事件参数
2
3子组件也可以通过绑定的事件向上传递参数,在自定义事件上添加传递参数的示例如下:
4
5
6```
7<!-- comp.hml -->
8<div class="item">
9   <text class="text-style" onclick="childClicked">点击这里查看隐藏文本</text>
10   <text class="text-style" if="{{showObj}}">hello world</text>
11</div>
12```
13
14
15```
16// comp.js
17export default {
18  childClicked () {
19    this.$emit('eventType1', {text: '收到子组件参数'});
20    this.showObj = !this.showObj;
21  },
22}
23```
24
25
26子组件向上传递参数text,父组件接收时通过e.detail来获取参数:
27
28
29```
30<!-- xxx.hml -->
31<div class="container">
32   <text>父组件:{{text}}</text>
33   <comp @event-type1="textClicked"></comp>
34</div>
35```
36
37
38```
39// xxx.js
40export default {
41  data: {
42    text: '开始',
43  },
44  textClicked (e) {
45    this.text = e.detail.text;
46  },
47}
48```
49