Skip to content

Commit dbd90a2

Browse files
mateuszRybczoneksdras
authored andcommitted
[Cookbook] Practical use of scoped slots with GoogleMaps (#1754)
* initial version * minor corrections * add codesandbox link * corrections * further corrections * apply CR corrections * apply CR suggestions * set proper order value
1 parent 29d3a77 commit dbd90a2

File tree

1 file changed

+389
-0
lines changed

1 file changed

+389
-0
lines changed
Lines changed: 389 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,389 @@
1+
---
2+
title: Practical use of scoped slots with GoogleMaps
3+
type: cookbook
4+
order: 14
5+
---
6+
7+
## Base Example
8+
9+
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.
10+
11+
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.
12+
13+
We'll create a component (`GoogleMapLoader.vue`) that:
14+
1. Initializes the [Google Maps API](https://developers.google.com/maps/documentation/javascript/reference/)
15+
2. Creates `google` and `map` objects
16+
3. Exposes those objects to the parent component in which the `GoogleMapLoader` is used
17+
18+
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.
19+
20+
Let’s first establish our `GoogleMapLoader.vue` template:
21+
22+
```html
23+
<template>
24+
<div>
25+
<div class="google-map" ref="googleMap"></div>
26+
<template v-if="Boolean(this.google) && Boolean(this.map)">
27+
<slot
28+
:google="google"
29+
:map="map"
30+
/>
31+
</template>
32+
</div>
33+
</template>
34+
```
35+
36+
Now, our script needs to pass some props to the component which allows us to set the [Google Maps API](https://developers.google.com/maps/documentation/javascript/reference/) and [Map object](https://developers.google.com/maps/documentation/javascript/reference/map#Map):
37+
38+
```js
39+
import GoogleMapsApiLoader from "google-maps-api-loader"
40+
41+
export default {
42+
props: {
43+
mapConfig: Object,
44+
apiKey: String,
45+
},
46+
47+
data() {
48+
return {
49+
google: null,
50+
map: null
51+
}
52+
},
53+
54+
async mounted() {
55+
const googleMapApi = await GoogleMapsApiLoader({
56+
apiKey: this.apiKey
57+
})
58+
this.google = googleMapApi
59+
this.initializeMap()
60+
},
61+
62+
methods: {
63+
initializeMap() {
64+
const mapContainer = this.$refs.googleMap)
65+
this.map = new this.google.maps.Map(
66+
mapContainer, this.mapConfig
67+
)
68+
}
69+
}
70+
}
71+
```
72+
73+
This is just part of a working example, you can find the whole example in the Codesandbox below.
74+
75+
<iframe src="https://codesandbox.io/embed/1o45zvxk0q" style="width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;" sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"></iframe>
76+
77+
## Real-World Example: Creating a Google Map Loader component
78+
79+
### 1. Create a component that initializes our map
80+
81+
`GoogleMapLoader.vue`
82+
83+
In the template, we create a container for the map which will be used to mount the [Map](https://developers.google.com/maps/documentation/javascript/reference/map#Map) object extracted from the Google Maps API.
84+
85+
```html
86+
<template>
87+
<div>
88+
<div class="google-map" ref="googleMap"></div>
89+
</div>
90+
</template>
91+
```
92+
93+
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:
94+
95+
- [mapConfig](https://developers.google.com/maps/documentation/javascript/reference/3/map#MapOptions): Google Maps config object
96+
- [apiKey](https://developers.google.com/maps/documentation/javascript/get-api-key): Our personal api key required by Google Maps
97+
98+
```js
99+
import GoogleMapsApiLoader from "google-maps-api-loader"
100+
101+
export default {
102+
props: {
103+
mapConfig: Object,
104+
apiKey: String,
105+
},
106+
```
107+
Then, we set the initial values of google and map to null:
108+
109+
```js
110+
data() {
111+
return {
112+
google: null,
113+
map: null
114+
}
115+
},
116+
```
117+
118+
On `mounted` hook we instantiate a `googleMapApi` and `Map` objects from the `GoogleMapsApi` and we set the values of `google` and `map` to the created instances:
119+
120+
```js
121+
async mounted() {
122+
const googleMapApi = await GoogleMapsApiLoader({
123+
apiKey: this.apiKey
124+
})
125+
this.google = googleMapApi
126+
this.initializeMap()
127+
},
128+
129+
methods: {
130+
initializeMap() {
131+
const mapContainer = this.$refs.googleMap
132+
this.map = new this.google.maps.Map(mapContainer, this.mapConfig)
133+
}
134+
}
135+
}
136+
```
137+
138+
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.
139+
140+
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.
141+
142+
To achieve that, we need to allow the parent component that will use our `GoogleMapLoader` to access `this.google` and `this.map` that are set inside the `GoogleMapLoader` component. That’s where [scoped slots](https://vuejs.org/v2/guide/components-slots.html#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.
143+
144+
### 2. Create component that uses our initializer component.
145+
146+
`TravelMap.vue`
147+
148+
In the template, we render the `GoogleMapLoader` component and pass props that are required to initialize the map.
149+
150+
```html
151+
<template>
152+
<GoogleMapLoader
153+
:mapConfig="mapConfig"
154+
apiKey="yourApiKey"
155+
/>
156+
</template>
157+
```
158+
159+
Our script tag will look like this:
160+
161+
```js
162+
<script>
163+
import GoogleMapLoader from './GoogleMapLoader'
164+
import { mapSettings } from '@/constants/mapSettings'
165+
166+
export default {
167+
components: {
168+
GoogleMapLoader
169+
},
170+
171+
computed: {
172+
mapConfig () {
173+
return {
174+
...mapSettings,
175+
center: { lat: 0, lng: 0 }
176+
}
177+
},
178+
},
179+
}
180+
</script>
181+
```
182+
183+
Still no scoped slots, so let's add one.
184+
185+
### 3. Expose `google` and `map` properties to the parent component by adding a scoped slot.
186+
187+
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.
188+
189+
`GoogleMapLoader.vue`
190+
191+
```html
192+
<template>
193+
<div>
194+
<div class="google-map" ref="googleMap"></div>
195+
<template v-if="Boolean(this.google) && Boolean(this.map)">
196+
<slot
197+
:google="google"
198+
:map="map"
199+
/>
200+
</template>
201+
</div>
202+
</template>
203+
```
204+
205+
Now, when we have the slot in the child component, we need to receive and consume the exposed props in the parent component.
206+
207+
### 4. Receive exposed props in the parent component using `slot-scope` attribute.
208+
209+
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](https://vuejs.org/v2/guide/components-slots.html#Destructuring-slot-scope) and only what we need.
210+
211+
Let’s de-structure this thing to get what we need.
212+
213+
`TravelMap.vue`
214+
215+
```html
216+
<GoogleMapLoader
217+
:mapConfig="mapConfig"
218+
apiKey="yourApiKey"
219+
>
220+
<template slot-scope="{ google, map }">
221+
{{ map }}
222+
{{ google }}
223+
</template>
224+
</GoogleMapLoader>
225+
```
226+
227+
Even though the `google` and `map` props do not exist in the `TravelMap` scope, the component has access to them and we can use them in the template.
228+
229+
You might wonder why would we do things like that and what is the use of all that?
230+
231+
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.
232+
233+
### 5. Create factory components for Markers and Polylines
234+
235+
Now when we have our map ready we will create two factory components that will be used to add elements to the `TravelMap`.
236+
237+
`GoogleMapMarker.vue`
238+
239+
```js
240+
import { POINT_MARKER_ICON_CONFIG } from "@/constants/mapSettings"
241+
242+
export default {
243+
props: {
244+
google: {
245+
type: Object,
246+
required: true
247+
},
248+
map: {
249+
type: Object,
250+
required: true
251+
},
252+
marker: {
253+
type: Object,
254+
required: true
255+
}
256+
},
257+
258+
mounted() {
259+
new this.google.maps.Marker({
260+
position: this.marker.position,
261+
marker: this.marker,
262+
map: this.map,
263+
icon: POINT_MARKER_ICON_CONFIG
264+
})
265+
}
266+
}
267+
```
268+
269+
`GoogleMapLine.vue`
270+
271+
```js
272+
import { LINE_PATH_CONFIG } from "@/constants/mapSettings"
273+
274+
export default {
275+
props: {
276+
google: {
277+
type: Object,
278+
required: true
279+
},
280+
map: {
281+
type: Object,
282+
required: true
283+
},
284+
path: {
285+
type: Array,
286+
required: true
287+
}
288+
},
289+
290+
mounted() {
291+
new this.google.maps.Polyline({
292+
path: this.path,
293+
map: this.map,
294+
...LINE_PATH_CONFIG
295+
})
296+
}
297+
}
298+
```
299+
300+
Both of these receive `google` that we use to extract the required object (Marker or Polyline) as well as `map` which gives as a reference to the map on which we want to place our element.
301+
302+
Each component also expects an extra prop to create a corresponding element. In this case, we have `marker` and `path`, respectively.
303+
304+
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.
305+
306+
There’s still one more step to go...
307+
308+
### 6. Add elements to map
309+
310+
Let’s use our factory components to add elements to our map. We must render the factory component and pass the `google` and `map` objects so data flows to the right places.
311+
312+
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.
313+
314+
Here we go, integrating the data points directly into the template:
315+
316+
```html
317+
<GoogleMapLoader
318+
:mapConfig="mapConfig"
319+
apiKey="yourApiKey"
320+
>
321+
<template slot-scope="{ google, map }">
322+
<GoogleMapMarker
323+
v-for="marker in markers"
324+
:key="marker.id"
325+
:marker="marker"
326+
:google="google"
327+
:map="map"
328+
/>
329+
<GoogleMapLine
330+
v-for="line in lines"
331+
:key="line.id"
332+
:path.sync="line.path"
333+
:google="google"
334+
:map="map"
335+
/>
336+
</template>
337+
</GoogleMapLoader>
338+
```
339+
340+
We need to import the required factory components in our script and set the data that will be passed to the markers and lines:
341+
342+
```js
343+
import {
344+
mapSettings
345+
} from '@/constants/mapSettings'
346+
347+
export default {
348+
components: {
349+
GoogleMapLoader,
350+
GoogleMapMarker,
351+
GoogleMapLine
352+
},
353+
354+
data () {
355+
return {
356+
markers: [
357+
{ id: "a", position: { lat: 3, lng: 101 } },
358+
{ id: "b", position: { lat: 5, lng: 99 } },
359+
{ id: "c", position: { lat: 6, lng: 97 } },
360+
],
361+
lines: [
362+
{ id: '1', path: [{ lat: 3, lng: 101 }, { lat: 5, lng: 99 }] },
363+
{ id: '2', path: [{ lat: 5, lng: 99 }, { lat: 6, lng: 97 }] }
364+
],
365+
}
366+
},
367+
368+
computed: {
369+
mapConfig () {
370+
return {
371+
...mapSettings,
372+
center: this.mapCenter
373+
}
374+
},
375+
376+
mapCenter () {
377+
return this.markers[1].position
378+
}
379+
},
380+
}
381+
```
382+
383+
## When To Avoid This Pattern
384+
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.
385+
386+
## Wrapping Up
387+
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 `GoogleMapLoader` component.
388+
389+
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.

0 commit comments

Comments
 (0)