标签:eve dem 参考 https class 组件 ESS data gif
组件可以使用Event Emitter装饰器发送数据和事件。
参考:
import { Event, EventEmitter } from ‘@stencil/core‘;
...
export class TodoList {
@Event() todoCompleted: EventEmitter;
todoCompletedHandler(todo: Todo) {
this.todoCompleted.emit(todo);
}
}
可以监听,特定元素的事件
参考:
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‘);
}
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
标签:eve dem 参考 https class 组件 ESS data gif
原文地址:https://www.cnblogs.com/rongfengliang/p/9710668.html