@onEvent
A decorator to subscribe to an event on the target element. The event listener will be automatically unsubscribed when the element is disconnected.
Please note the method binding is not necessary, as the method is bound to the instance of the class by default.
It accepts an object with one of the following properties:
selector
: A CSS selector to match the target element.ref
: A reference to the target element.window
: (true) Listen to the event on the window object.document
: (true) Listen to the event on the document object.
And the following properties:
type
: The event type to listen for.options
(optional):AddEventListenerOptions
An options object that specifies characteristics about the event listener.
@onEvent({ selector: "button", type: "click" }) doSomething() {...}
@onEvent({ ref: "my-ref", type: "click" }) doSomething() {...}
@onEvent({ window: true, type: "resize" }) doSomething() {...}
@onEvent({ document: true, type: "keydown" }) doSomething() {...}