Understanding magic properties using a Train Station analogy.
No actions yet. Interact with the demos below.
Analogy: The Current Platform
$el refers to the current DOM element. A platform can announce its own status without needing directions. It acts on itself.
Platform 3
Status: Awaiting Train
<button @click="announceDeparture($el)">...</button>
Analogy: Named Facilities
$refs lets you access other DOM elements by a name you give them (with x-ref). The control panel can target and operate specific, named facilities.
Use the central control panel to manage facilities:
// JS: this.$refs.ticketBooth.classList...
// HTML: <div x-ref="ticketBooth">
Analogy: The Entire Station
$root refers to the top-level DOM element of the component. A station-wide alert affects the entire component, no matter where the alert is triggered from.
Trigger an event that affects the entire station component (the gray container with the title).
Station is in alert mode!
// JS, from anywhere in the component:
this.$root.classList.add('station-alert');
"I'm talking to myself."
(The current element)
"I'm talking to that specific thing by name."
(A named element elsewhere in the component)
"I'm talking to everyone in the whole station."
(The component's top-level element)