- 인스턴스 프로퍼티 추가하기
- Form Validation
- Editable SVG Icon Systems
- Create a CMS-Powered Blog
- Unit Testing Vue Components
- Creating Custom Scroll Directives
- Debugging in VS Code
- Using Axios to Consume APIs
- Avoiding Memory Leaks
- Client-Side Storage
- Packaging Vue Components for npm
- Dockerize Vue.js App
- Practical use of scoped slots with GoogleMaps
Practical use of scoped slots with GoogleMaps
There are situations when you want the template inside the slot to be able to access data from the child component that is responsible for rendering the slot content. This is particularly useful when you need freedom in creating custom templates that use the child component’s data properties. That is a typical use case for scoped slots.
Imagine a component that configures and prepares an external API to be used in another component, but is not tightly coupled with any specific template. Such a component could then be reused in multiple places rendering different templates but using the same base object with specific API.
We’ll create a component (
- Initializes the Google Maps API
- Exposes those objects to the parent component in which the
Below is an example of how this can be achieved. We will analyze the code piece-by-piece and see what is actually happening in the next section.
Let’s first establish our
This is just part of a working example, you can find the whole example in the Codesandbox below.
In the template, we create a container for the map which will be used to mount the Map object extracted from the Google Maps API.
Next up, our script needs to receive props from the parent component which will allow us to set the Google Map. Those props consist of:
Then, we set the initial values of google and map to null:
mounted hook we instantiate a
Map objects from the
GoogleMapsApi and we set the values of
map to the created instances:
So far, so good. With all that done, we could continue adding the other objects to the map (Markers, Polylines, etc.) and use it as an ordinary map component.
But, we want to use our
GoogleMapLoader component only as a loader that prepares the map — we don’t want to render anything on it.
To achieve that, we need to allow the parent component that will use our
GoogleMapLoader to access
this.map that are set inside the
GoogleMapLoader component. That’s where scoped slots really shine. Scoped slots allow us to expose the properties set in a child component to the parent component. It may sound like Inception, but bear with me one more minute as we break that down further.
In the template, we render the
GoogleMapLoader component and pass props that are required to initialize the map.
Our script tag will look like this:
Still no scoped slots, so let’s add one.
Finally, we can add a scoped slot that will do the job and allow us to access the child component props in the parent component. We do that by adding the
<slot> tag in the child component and passing the props that we want to expose (using
v-bind directive or
:propName shorthand). It does not differ from passing the props down to the child component, but doing it in the
<slot> tag will reverse the direction of data flow.
Now, when we have the slot in the child component, we need to receive and consume the exposed props in the parent component.
To receive the props in the parent component, we declare a template element and use the
slot-scope attribute. This attribute has access to the object carrying all the props exposed from the child component. We can grab the whole object or we can de-structure that object and only what we need.
Let’s de-structure this thing to get what we need.
Even though the
map props do not exist in the
TravelMap scope, the component has access to them and we can use them in the template.
You might wonder why would we do things like that and what is the use of all that?
Scoped slots allow us to pass a template to the slot instead of a rendered element. It’s called a
scoped slot because it will have access to certain child component data even though the template is rendered in the parent component scope. This gives us the freedom to fill the template with custom content from the parent component.
Now when we have our map ready we will create two factory components that will be used to add elements to the
Both of these receive
map which gives as a reference to the map on which we want to place our element.
Each component also expects an extra prop to create a corresponding element. In this case, we have
On the mounted hook, we create an element (Marker/Polyline) and attach it to our map by passing the
map property to the object constructor.
There’s still one more step to go…
Let’s use our factory components to add elements to our map. We must render the factory component and pass the
map objects so data flows to the right places.
We also need to provide the data that’s required by the element itself. In our case, that’s the
marker object with the position of the marker and the
path object with Polyline coordinates.
Here we go, integrating the data points directly into the template:
We need to import the required factory components in our script and set the data that will be passed to the markers and lines:
It might be tempting to create a very complex solution based on the example, but at some point we can get to the situation where this abstraction becomes an independent part of the code living in our codebase. If we get to that point it might be worth considering extraction to an add-on.
That’s it. With all those bits and pieces created we can now re-use the
GoogleMapLoader component as a base for all our maps by passing different templates to each one of them. Imagine that you need to create another map with different Markers or just Markers without Polylines. By using the above pattern it becomes very easy as we just need to pass different content to the
This pattern is not strictly connected to Google Maps; it can be used with any library to set the base component and expose the library’s API that might be then used in the component that summoned the base component.