25.06.2018 Views

Full-Stack Vue.js 2 and Laravel 5

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Scoped slots<br />

The FeatureList component works but is quite weak. The majority of the content<br />

comes through the slot <strong>and</strong> so it seems like the parent is doing too much work,<br />

<strong>and</strong> the child too little. Given that there's repeated code in both declarations of<br />

the component (), it'd be good to delegate this to<br />

the child.<br />

To allow our component template to be more versatile, we can use a scoped slot<br />

instead of a regular slot. Scoped slots allow you to pass a template to the slot<br />

instead of passing a rendered element. When this template is declared in the<br />

parent, it will have access to any props supplied in the child.<br />

For example, a component child with a scoped slot might look like the following:<br />

<br />

A parent that uses this component will declare a template element, which will<br />

have a property slot-scope that names an alias object. Any props added to the slot<br />

in the child template are available as properties of the alias object: <br />

Hello from parent {{<br />

props.my-prop }} <br />

This renders as:<br />

<br />

Hello from parent<br />

Hello from child<br />

<br />

Let's go through the steps of including a scoped slot with our FeatureList<br />

component. The goal is to be able to pass the list items array in as a prop <strong>and</strong> get<br />

the FeatureList component to iterate them. That way, FeatureList is taking<br />

ownership of any repeated functionality. The parent will then provide a template<br />

to define how each list item should display.<br />

resources/views/app.blade.php:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!