组件可以使用Event Emitter装饰器发送数据和事件。

Event 定义

参考:
import { Event, EventEmitter } from '@stencil/core'; ...
export class TodoList { @Event() todoCompleted: EventEmitter; todoCompletedHandler(todo: Todo) {
this.todoCompleted.emit(todo);
}
}

监听事件(Listen)

可以监听,特定元素的事件

参考:
import { Listen } from '@stencil/core'; ...
export class TodoApp { @Listen('todoCompleted')
todoCompletedHandler(event: CustomEvent) {
console.log('Received the custom todoCompleted event: ', event.detail);
}
}

监听键盘事件

@Listen('keydown')
handleKeyDown(ev){
if(ev.keyCode === 40){
console.log('down arrow pressed')
}
} @Listen('keydown.up')
handleUpArrow(ev){
console.log('will fire when up arrow is pressed');
}

jsx 中使用事件

import { Event, EventEmitter } from '@stencil/core';

...
export class TodoList { @Event() todoCompleted: EventEmitter; todoCompletedHandler(todo: Todo) {
this.todoCompleted.emit(todo);
}
} <todo-list onTodoCompleted={ev => this.someMethod(ev)}></todo-list>

参考例子

  • 子组件
import { Component,Event,EventEmitter, State} from '@stencil/core';

@Component({
tag: 'my-child-component',
styleUrl: 'my-child-component.css',
shadow: false,
scoped: false
}) export class MyChildComponent { @Event() userClickEvent: EventEmitter;
UserClickHandler() {
this.userClickEvent.emit({name:"dalong",age:44});
}
@State() name:String="demoapp"
render(){
return (
<div>
<p onClick={_=>this.UserClickHandler()}>click</p>
<p>child component</p>
</div>
);
}
}
  • 父组件
import { Component,State, Prop} from '@stencil/core';

@Component({
tag: 'my-component',
styleUrl: 'my-component.css',
shadow: false,
})
export class MyComponent { // @Listen("userClickEvent")
// getUserClickEvent(event: CustomEvent){
// this.name=JSON.stringify(event.detail)
// }
something(event:CustomEvent){
this.name=JSON.stringify(event.detail)
}
@State() name:String="first info"
@Prop() UserID:String="rong"
render() {
return (
<div >
<p>{this.UserID}</p>
<p>{this.name}</p>
<my-child-component onUserClickEvent={env=>this.something(env)}></my-child-component>
</div>
);
}
}
  • 事件绑定
使用Listen
@Listen("userClickEvent")
getUserClickEvent(event: CustomEvent){
this.name=JSON.stringify(event.detail)
}
使用jsx 绑定:
<my-child-component onUserClickEvent={env=>this.something(env)}></my-child-component>
something(event:CustomEvent){
this.name=JSON.stringify(event.detail)
}

参考资料

https://stenciljs.com/docs/events

 
 
 
 

stenciljs 学习五 事件的更多相关文章

  1. salesforce lightning零基础学习(五) 事件阶段(component events phase)

    上一篇介绍了lightning component events的简单介绍.此篇针对上一篇进行深入,主要讲的内容为component event中的阶段(Phase). 一. 阶段(Phase)的概念 ...

  2. 从零开始学习jQuery (五) 事件与事件对象

    本系列文章导航 从零开始学习jQuery (五) 事件与事件对象 一.摘要 事件是脚本编程的灵魂. 所以本章内容也是jQuery学习的重点. 本文将对jQuery中的事件处理以及事件对象进行详细的讲解 ...

  3. cesium 学习(五) 加载场景模型

    cesium 学习(五) 加载场景模型 一.前言 现在开始实际的看看效果,目前我所接触到基本上都是使用Cesium加载模型这个内容,以及在模型上进行操作.So,现在进行一些加载模型的学习,数据的话可以 ...

  4. TweenMax动画库学习(五)

    目录            TweenMax动画库学习(一)            TweenMax动画库学习(二)            TweenMax动画库学习(三)            Tw ...

  5. SVG 学习<五> SVG动画

    目录 SVG 学习<一>基础图形及线段 SVG 学习<二>进阶 SVG世界,视野,视窗 stroke属性 svg分组 SVG 学习<三>渐变 SVG 学习<四 ...

  6. Android JNI学习(五)——Demo演示

    本系列文章如下: Android JNI(一)——NDK与JNI基础 Android JNI学习(二)——实战JNI之“hello world” Android JNI学习(三)——Java与Nati ...

  7. ZigBee学习五 无线温度检测

    ZigBee学习五 无线温度检测 1)修改公用头文件GenericApp.h typedef union h{ uint8 TEMP[4]; struct RFRXBUF { unsigned cha ...

  8. (转)MyBatis框架的学习(五)——一对一关联映射和一对多关联映射

    http://blog.csdn.net/yerenyuan_pku/article/details/71894172 在实际开发中我们不可能只是对单表进行操作,必然要操作多表,本文就来讲解多表操作中 ...

  9. Vue – 基础学习(4):事件修饰符

    Vue – 基础学习(3):事件修饰符

随机推荐

  1. 20170617xlVBA调查问卷基础数据分类计数

    Public Sub GatherDataPicker() Application.ScreenUpdating = False Application.DisplayAlerts = False A ...

  2. Laravel JsonResponse数组获取

    有一个JsonResponse数据的格式如下: object(Illuminate\Http\JsonResponse)[474] protected 'data' => string '{&q ...

  3. 『Collections』Counter计数

    Counter()方法 计数器,返回字典,会同时去重,文本处理常用 from collections import Counter co = Counter(list('abcdefgad')) co ...

  4. Superset

    Superset是一款可自助.可交互,可视化非常不错的产品 Superset is a data exploration platform designed to be visual, intuiti ...

  5. CSS3动画和JS动画的比较

    前言 之前有被问到一个问题,css3动画和js动画性能谁更好,为什么.据我的经验,当然觉得css3动画性能更好,至于为什么一时还真答不上来,所以特意查了一下资料总结一波. JS动画 优点: js动画控 ...

  6. popen strtok 函数的使用

    FILE * popen ( const char * command , const char * type ); int pclose ( FILE * stream );   type 参数只能 ...

  7. en_a

      1◆ai ay ei     2◆ au aw ɔː     3◆ a   eɪ æ ɑː ɔː ʌ   4◆ ar   ɔː ɑː ɜː   5◆ al ɑː ɔː   6◆ are air   ...

  8. win10解除密码

  9. 【转载】oracle之rowid详解

    原文URL:http://www.2cto.com/database/201109/104961.html 本文讨论的是关于oracle从8i开始引进object的概念后的rowid,即扩展(exte ...

  10. struts1的parameter

    1.配置文件    parameter="method" 2.请求路径      http://localhost:8081/purchaseDeclareAction.do?me ...