Created
January 9, 2021 16:02
-
-
Save hallvors/5340f94cb2ad3ab83b9bd0246a4f5b5e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
// Based on code from https://stackoverflow.com/questions/59629947/how-do-i-load-an-external-js-library-in-svelte-sapper | |
import { onMount, createEventDispatcher } from "svelte"; | |
const dispatch = createEventDispatcher(); | |
export let src; | |
export let libraryDetectionObject; | |
let script; | |
onMount(() => { | |
if ( | |
libraryDetectionObject && | |
window && | |
typeof window[libraryDetectionObject] !== "undefined" | |
) { | |
return dispatch("loaded"); | |
} | |
script.addEventListener("load", () => { | |
console.log("load event from script"); | |
dispatch("loaded"); | |
}); | |
script.addEventListener("error", (event) => { | |
console.error("something went wrong", event); | |
dispatch("error"); | |
}); | |
}); | |
</script> | |
<svelte:head> | |
<script bind:this={script} {src}> | |
</script> | |
</svelte:head> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Doesnt work, svelte doesnt recognise the script tag in head