Performance: Directive with arguments and modifiers
This affects how Vue processes directives during rendering, impacting the number of DOM updates and reflows triggered by directive logic.
app.directive('focus', { mounted(el, binding) { const { arg, modifiers } = binding; if (arg === 'input' && modifiers.lazy) { const handler = () => el.focus(); el.addEventListener('change', handler, { once: true }); } else if (arg === 'button' && modifiers.immediate) { el.focus(); } } });
app.directive('focus', { mounted(el, binding) { if (binding.arg === 'input' && binding.modifiers.lazy) { el.addEventListener('change', () => { el.focus(); }); } else if (binding.arg === 'button' && binding.modifiers.immediate) { el.focus(); } } });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Directive with multiple modifiers and dynamic arguments causing repeated event listeners | Multiple event listeners per element | Multiple reflows per event | High paint cost due to frequent focus calls | [X] Bad |
| Directive with optimized event listener usage and conditional logic | Single event listener per element | Single reflow per event | Lower paint cost with limited focus calls | [OK] Good |
v-bind:title uses title as an argument. What does it do?v-bind:titletitle is the argument telling v-bind to bind the value to the element's title attribute.v-on:click.prevent is an example. Which is correct?v-on:click.prevent.v-on:click.prevent matches the correct pattern: directive, colon, argument, dot, modifier. Others mix order or add parentheses incorrectly.<button v-on:click.once.prevent="submit">Submit</button>
.once and .prevent.once makes the event handler run only once. .prevent calls event.preventDefault() to stop default browser behavior.submit only once and prevents default action each time..once + .prevent = run once + prevent default [OK]<input v-model.trim.lazy="username">
.trim and .lazy on v-model to trim input and update on change.v-model.trim.lazy is valid and commonly used. No argument is needed here.v-focus that focuses an input only once when inserted, and you want to pass an argument to specify the delay in milliseconds. Which is the correct way to use arguments and modifiers to achieve this?<input v-focus:delay.once="500">
.once change behavior, here to run focus only once.v-focus:delay.once="500" correctly uses 'delay' as argument and '.once' as modifier. The value '500' is passed as delay time.delay for delay and modifier .once to focus once. -> Option APass the test and this pattern moves unknown