An interactive demonstration of $el, $refs, and $root
Analogy: A chef station is self-aware. A button at the station can act upon the station itself (e.g., "clean me," "turn on my heat lamp"). $el refers to the current DOM element.
<button @click="cook($el.closest('.station'))">Cook</button>
Status:
Analogy: The Head Chef uses a control panel to operate specific, named equipment from afar ("turn on the grill," "check the fryer"). $refs provides access to elements explicitly marked with x-ref.
this.$refs.grill.classList.add(...)
Head Chef's Control Panel
Analogy: A general announcement affects the entire kitchen. "Service Start!" puts the whole operation on high alert. $root refers to the top-level DOM element of the component.
this.$root.classList.add('active-service')
(Watch for the border/shadow on the entire white component area)
| Magic Property | Analogy | Scope | Use Case |
|---|---|---|---|
$el |
The Chef Station | The single DOM element the directive is on. | Self-manipulation. A button that disables itself, an input that focuses itself, or passing itself to a method. |
$refs |
Named Equipment | Any element within the component with an x-ref attribute. |
Accessing specific, distant elements from your component logic, like controlling a video player or focusing a specific input from a button. |
$root |
The Entire Kitchen | The component's top-most DOM element (where x-data is defined). |
Applying a state to the entire component, like a 'loading' or 'success' visual state on the whole container. |