File tree Expand file tree Collapse file tree 3 files changed +70
-71
lines changed
examples/16-context/00-context-api
tutorial/15-context/01-context-api Expand file tree Collapse file tree 3 files changed +70
-71
lines changed Original file line number Diff line number Diff line change 1
1
<script >
2
- import { onMount , setContext } from ' svelte' ;
2
+ import { onDestroy , setContext } from ' svelte' ;
3
3
import { mapbox , key } from ' ./mapbox.js' ;
4
4
5
5
setContext (key, {
6
- getMap : () => map
6
+ getMap : () => map,
7
7
});
8
8
9
9
export let lat;
13
13
let container;
14
14
let map;
15
15
16
- onMount (() => {
17
- const link = document .createElement (' link' );
18
- link .rel = ' stylesheet' ;
19
- link .href = ' https://unpkg.com/mapbox-gl/dist/mapbox-gl.css' ;
20
-
21
- link .onload = () => {
22
- map = new mapbox.Map ({
23
- container,
24
- style: ' mapbox://styles/mapbox/streets-v9' ,
25
- center: [lon, lat],
26
- zoom
27
- });
28
- };
29
-
30
- document .head .appendChild (link);
31
-
32
- return () => {
33
- map .remove ();
34
- link .parentNode .removeChild (link);
35
- };
16
+ function load () {
17
+ map = new mapbox.Map ({
18
+ container,
19
+ style: ' mapbox://styles/mapbox/streets-v9' ,
20
+ center: [lon, lat],
21
+ zoom,
22
+ });
23
+ }
24
+
25
+ onDestroy (() => {
26
+ if (map) map .remove ();
36
27
});
37
28
</script >
38
29
30
+ <svelte:head >
31
+ <link
32
+ rel =" stylesheet"
33
+ href =" https://unpkg.com/mapbox-gl/dist/mapbox-gl.css"
34
+ on:load ={load }
35
+ />
36
+ </svelte:head >
37
+
39
38
<div bind:this ={container }>
40
39
{#if map }
41
- <slot ></ slot >
40
+ <slot / >
42
41
{/if }
43
42
</div >
44
43
47
46
width : 100% ;
48
47
height : 100% ;
49
48
}
50
- </style >
49
+ </style >
Original file line number Diff line number Diff line change 1
1
<script >
2
- import { onMount } from ' svelte' ;
2
+ import { onDestroy } from ' svelte' ;
3
3
import { mapbox } from ' ./mapbox.js' ;
4
4
5
5
// set the context here...
11
11
let container;
12
12
let map;
13
13
14
- onMount (() => {
15
- const link = document .createElement (' link' );
16
- link .rel = ' stylesheet' ;
17
- link .href = ' https://unpkg.com/mapbox-gl/dist/mapbox-gl.css' ;
18
-
19
- link .onload = () => {
20
- map = new mapbox.Map ({
21
- container,
22
- style: ' mapbox://styles/mapbox/streets-v9' ,
23
- center: [lon, lat],
24
- zoom
25
- });
26
- };
27
-
28
- document .head .appendChild (link);
29
-
30
- return () => {
31
- map .remove ();
32
- link .parentNode .removeChild (link);
33
- };
14
+ function load () {
15
+ map = new mapbox.Map ({
16
+ container,
17
+ style: ' mapbox://styles/mapbox/streets-v9' ,
18
+ center: [lon, lat],
19
+ zoom,
20
+ });
21
+ }
22
+
23
+ onDestroy (() => {
24
+ if (map) map .remove ();
34
25
});
35
26
</script >
36
27
28
+ <!-- this special element will be explained in a later section -->
29
+ <svelte:head >
30
+ <link
31
+ rel =" stylesheet"
32
+ href =" https://unpkg.com/mapbox-gl/dist/mapbox-gl.css"
33
+ on:load ={load }
34
+ />
35
+ </svelte:head >
36
+
37
37
<div bind:this ={container }>
38
38
{#if map }
39
- <slot ></ slot >
39
+ <slot / >
40
40
{/if }
41
41
</div >
42
42
45
45
width : 100% ;
46
46
height : 100% ;
47
47
}
48
- </style >
48
+ </style >
Original file line number Diff line number Diff line change 1
1
<script >
2
- import { onMount , setContext } from ' svelte' ;
2
+ import { onDestroy , setContext } from ' svelte' ;
3
3
import { mapbox , key } from ' ./mapbox.js' ;
4
4
5
5
setContext (key, {
6
- getMap : () => map
6
+ getMap : () => map,
7
7
});
8
8
9
9
export let lat;
13
13
let container;
14
14
let map;
15
15
16
- onMount (() => {
17
- const link = document .createElement (' link' );
18
- link .rel = ' stylesheet' ;
19
- link .href = ' https://unpkg.com/mapbox-gl/dist/mapbox-gl.css' ;
20
-
21
- link .onload = () => {
22
- map = new mapbox.Map ({
23
- container,
24
- style: ' mapbox://styles/mapbox/streets-v9' ,
25
- center: [lon, lat],
26
- zoom
27
- });
28
- };
29
-
30
- document .head .appendChild (link);
31
-
32
- return () => {
33
- map .remove ();
34
- link .parentNode .removeChild (link);
35
- };
16
+ function load () {
17
+ map = new mapbox.Map ({
18
+ container,
19
+ style: ' mapbox://styles/mapbox/streets-v9' ,
20
+ center: [lon, lat],
21
+ zoom,
22
+ });
23
+ }
24
+
25
+ onDestroy (() => {
26
+ if (map) map .remove ();
36
27
});
37
28
</script >
38
29
30
+ <!-- this special element will be explained in a later section -->
31
+ <svelte:head >
32
+ <link
33
+ rel =" stylesheet"
34
+ href =" https://unpkg.com/mapbox-gl/dist/mapbox-gl.css"
35
+ on:load ={load }
36
+ />
37
+ </svelte:head >
38
+
39
39
<div bind:this ={container }>
40
40
{#if map }
41
- <slot ></ slot >
41
+ <slot / >
42
42
{/if }
43
43
</div >
44
44
47
47
width : 100% ;
48
48
height : 100% ;
49
49
}
50
- </style >
50
+ </style >
You can’t perform that action at this time.
0 commit comments