Vue Slots Explained
Slots are super-powerful in Vue.js. They let you inject components from a parent into a various parts of the child, helping you compose nice and generic components. But that’s not all! We’ll take a run-up to dynamic scoped slots through the following:
Btw, apologies for the syntax highlighting, it struggles a bit with Vue!
Vue Js Slots Explained, the star casino broadbeach accommodation, french lick casino odds, sweden casino flashback. Vue.js - The Progressive JavaScript Framework. How Changes Are Tracked. When you pass a plain JavaScript object to a Vue instance as its data option, Vue will walk through all of its properties and convert them to getter/setters using Object.defineProperty.This is an ES5-only and un-shimmable feature, which is why Vue. Vue 2.6 (v-slot syntax). Its value is set according to the data passed to the slot by the component. This is explained in the docs. Vue knows that it is a variable.
Default slots
This is the most basic of slots. It takes the content you provide in the child declaration: <comp>THIS BIT</comp>
, and injects it into the component.
This will render as:
Named slots
Named slots allow you to inject multiple bits of content within a child component:
Scoped slots
Scoped slots let you pass data/methods from the child component, back into the parent. It’s really useful for list rendering, where the child does some. It tells Vue which named slot to use, exposes the passed props, which we can destructure. This could be a method or data and works a bit like a render-function in React.
Dynamic scoped slots
Vue Render Slot
Right, the final piece of the puzzle. Dynamic scoped slots are much like regular scoped slots, but they have a name derived at runtime. How is that useful, you may ask…
Vue Slots Explained Game
Take a multi-step form. You want a wrapper element that’ll handle all the styling and step navigation, and you want to feed the steps as an array. Most steps have a consistent header, but a few of them need additional components to be rendered within. If this was React-land, we could pass full components in the array easily enough, but Vue makes this sorta thing a bit more tricksy. Let’s approach this from the outside, and define how we want to use this component before building it.
Cool, onto building this component:
With that intriguing looking <slot />
tag, we create a dynamic scoped slot that’ll only be rendered when a corresponding <template name='header_step-1'>
is used by the parent. We can still pass in custom data and methods like any other scoped slot.
Vue Template Slot
You could even hide the default header if a dynamic slot has been passed in with the following code:
Vue Slots Explained Games
Pretty neat!