A volte essere pigri è una qualità, evita (quasi) tutte le richieste di personalizzazioni lasciando "finestre aperte" nei tuoi componenti generici.
Gli slots sono un modo per passare il contenuto a un componente in Vue.js. Permettono di definire una sezione nel template di un componente che può essere sostituita dall'esterno.
È inoltre possibile assegnare un nome o uno "scope" agli slots, garantendo così maggiore controllo e personalizzazione sul contenuto. I "named slots" consentono di definire più slots nello stesso componente, assegnando dei nomi specifici.
Gli "scoped slots" ti consentono di accedere dall'esterno ai dati interni del componente.
Boost Fertility New Invention Ups Success Rates.pdf
Vue.js slots.pdf
1. VUE.JS SLOTS
Non farti sommergere dalle richieste di
personalizzazione!
COMMIT UNIVERSITY
Firenze, 19 Ottobre 2023
1
2. WHO AM I
Denny Biasiolli
Full Stack Developer
(JavaScript, Python, Go)
Front End Developer UX/ UI
Fingerprint Compliance Services Ltd.
Italy, Savigliano (CN)
@dennybiasiolli
denny.biasiolli@gmail.com
www.dennybiasiolli.com
2
31. WHAT ARE SLOTS IN VUE.JS?
A way to pass content to a component
30
32. USAGE OF SLOTS IN VUE.JS
1. define a section of a component’s template that can
be replaced by the parent component
2. the parent component controls the layout and
content of the child component
31
33. SLOT CONTENT AND OUTLET
1. allow a component to accept dynamic content
2. pass a template fragment to a child component
<button class="fancy-btn">
<slot></slot> <!-- slot outlet -->
</button>
<FancyButton>
Click me! <!-- slot content -->
</FancyButton>
32
34. SLOT CONTENT AND OUTLET
<button class="fancy-btn">Click me!</button>
33
35. SLOT CONTENT AND OUTLET
FancyButton is responsible for rendering the
outer <button> and its styling
the inner content (and its style) is provided by the
parent component
34
36. SLOT CONTENT AND OUTLET
Slot content is not just limited to text!
<FancyButton>
<span style="color:red">Click me!</span>
<AwesomeIcon name="plus" />
</FancyButton>
35
37. SLOT CONTENT AND OUTLET
<FancyButton> is flexible and reusable.
We can now use it in different places with different
inner content, but all with the same fancy styling.
36
38. RENDER SCOPE
Slot content
has access to the data scope of the parent
component
does not have access to the child component's data
<span>{{ message }}</span>
<FancyButton>{{ message }}</FancyButton>
37
39. FALLBACK CONTENT
Default value for a slot content
Rendered only when no content is provided
<button type="submit">
<slot>
Submit <!-- fallback content -->
</slot>
</button>
38
40. FALLBACK CONTENT
Examples
<SubmitButton />
<!-- rendered as -->
<button type="submit">Submit</button>
<SubmitButton>Save</SubmitButton>
<!-- rendered as -->
<button type="submit">Save</button>
39
41. NAMED SLOTS
Multiple slot outlets in the same component.
<div class="container">
<header>
<!-- We want header content here -->
</header>
<main>
<!-- We want main content here -->
</main>
<footer>
<!-- We want footer content here -->
</footer>
</div>
40
42. NAMED SLOTS
<slot> attribute name
<slot> without name
implicitly has the name "default".
<div class="container">
<header>
<slot name="header"></slot>
</header>
<main>
<slot></slot>
</main>
<footer>
<slot name="footer"></slot>
</footer>
</div>
41
43. PASSING NAMED SLOTS
<template> element with the v-slot directive
"#" can replace v-slot
<template v-slot:header>
<!-- content for the header slot -->
</template>
<template #header>
<!-- content for the header slot -->
</template>
42
45. PASSING NAMED SLOTS
<template #default> can be omitted
<BaseLayout>
<template #header>
<h1>Here might be a page title</h1>
</template>
<template #default>
<p>A paragraph for the main content.</p>
<p>And another one.</p>
</template>
<template #footer>
<p>Here's some contact info</p>
</template>
</BaseLayout>
44
46. PASSING NAMED SLOTS
is the same as
<BaseLayout>
<template #header>
<h1>Here might be a page title</h1>
</template>
<!-- implicit default slot -->
<p>A paragraph for the main content.</p>
<p>And another one.</p>
<template #footer>
<p>Here's some contact info</p>
</template>
</BaseLayout>
45
47. SCOPED SLOTS
Yes, but...
The parent component does not have
access to the child component's data
We can pass attributes to a slot outlet
46
48. SCOPED SLOTS
Passing attributes to a slot outlet
Receiving slot props in the template
<slot :text="greetingMessage" :count="1"></slot>
<MyComponent v-slot="slotProps">
{{ slotProps.text }} {{ slotProps.count }}
</MyComponent>
<!-- or -->
<MyComponent v-slot="{ text, count}">
{{ text }} {{ count }}
</MyComponent>
47
53. NAMED SCOPED SLOTS
Where is the error?
<template>
<MyComponent v-slot="{ message }">
<p>{{ message }}</p>
<template #footer>
<p>{{ message }}</p>
</template>
</MyComponent>
</template>
52
54. NAMED SCOPED SLOTS
Where is the error?
<template>
<MyComponent>
<template #default="{ message }">
<p>{{ message }}</p>
</template>
<template #footer>
<!-- message belongs to the default slot
it is not available here -->
<p>{{ message }}</p>
</template>
</MyComponent>
</template>
53