Guide: Event Handling Flashcards
What are the ways you can handle when a v-on
is triggered from a DOM event?
- Running inline Javascript
<button v-on:click="counter += 1">Add 1</button>
- Using the name of a method, with the native DOM event being the only argument
<button>Increase</button>
counter(event) {...
- Invoking a method inline, using the special variable,
$event
, as needed to pass the native DOM event<button>Delete</button>
What is the syntax description of an event modifier?
A directive postfix denoted by a dot.
What is the point of using event modifiers for v-on
?
So that we can leave purely data logic in the methods rather than having to deal with DOM event details.
Name 5 event modifiers for v-on
.
.stop
.prevent
.capture
.self
.once
.passive
Describe the difference between:
v-on:click.prevent.self
and
v-on:click.self.prevent
-
v-on:click.prevent.self
- This will prevent all clicks
-
v-on:click.self.prevent
- This will only prevent clicks on the element itself
How do you ensure a click handler will be triggered at most one time? And what’s special about it?
v-on:click.once="handler"
It can also be used on component events.
How can we improve mobile performance with scrolling if there’s a scroll handler?
What’s an important thing to remember?
Use v-on:scroll.passive="scrollHandler"
, which is telling the browser to not wait for the scrollHandler
to complete, and instead beginning the behavior immediately.
This is in essence saying, there’s no preventDefault()
in the method, so go ahead and do the default behavior.
Don’t use .prevent
with .passive
because using .passive
is inheritanly saying that we’re not going to prevent the default behavior.
What is the full list of key modifier aliases that can be used when v-on
is listening for keyboard events?
.enter
.tab
-
.delete
(captures both “Delete” and “Backspace”) .esc
.space
.up
.down
.left
.right
Do you have to resort to a using a keyCode
if there’s no alias for it?
No, you can define custom key modifier aliases via the global config.keyCodes
object
Vue.config.keyCodes.f1 = 112
What are automatic key modifiers?
They are the valid key names exposed via KeyboardEvent.key
but converted to kebab-case.
<input>
This would only work if $event.key === 'PageDown'
What are system modifiers, list all of them, and what’s an important rule about them?
They enable keyboard of mouse event listeners to be triggered only when the specified modifier key is pressed.
.ctrl
.alt
.shift
.meta
An importnat rule is that keyup.ctrl
will only trigger if you release some other key while holding down ctrl
. It won’t trigger by releasing ctrl
alone! To do that, use the keyCode
for ctrl
: keyup.17
What does exact do?
Describe what each one does:
- `<button>A</button>
<button>A</button>
<button>A</button>
It allows control over the exact combination of system modifiers needed to trigger an event.
- Will trigger on control-clicking, but will still trigger if some other modifier like
shift
is also pressed - Will trigger if the only system modifier pressed is Control while clicking
- Will trigger if no system modifier is pressed when clicking
What are the mouse button modifiers?
.left
.right
.middle
What are three advantages to having listeners in HTML?
- It’s easier to locate handler functions by skimming the HTML template for it’s definition
- Since there’s no manual attaching event listeners in JS, the ViewModel code can be pure logic and DOM-free. Much easier to test.
- When a ViewModel is destroyed, all event listeners are automatically removed.