You need to sign in or sign up before continuing.
js-components-custom-event-parameter.md 909 字节
Newer Older
Z
zengyawen 已提交
1
# 事件参数<a name="ZH-CN_TOPIC_0000001173164759"></a>
M
mamingshuai 已提交
2 3 4 5 6 7 8 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

子组件也可以通过绑定的事件向上传递参数,在自定义事件上添加传递参数的示例如下:

```
<!-- comp.hml -->
<div class="item">  
   <text class="text-style" onclick="childClicked">点击这里查看隐藏文本</text> 
   <text class="text-style" if="{{showObj}}">hello world</text> 
</div>
```

```
// comp.js
export default { 
  childClicked () {
    this.$emit('eventType1', {text: '收到子组件参数'});
    this.showObj = !this.showObj;
  },
}
```

子组件向上传递参数text,父组件接收时通过e.detail来获取参数:

```
<!-- xxx.hml -->
<div class="container">  
   <text>父组件:{{text}}</text> 
   <comp @event-type1="textClicked"></comp>  
</div>
```

```
// xxx.js
export default { 
  data: {
    text: '开始',
  },
  textClicked (e) {
    this.text = e.detail.text;
  },
}
```