This might possibly be how Svelte.js works, but I'm curious if I'm doing something wrong, or if there's a workaround.
If I set compiler option customElement: true
, properties passed to components are not available in the <script>
tag within that component. I use webpack with svelte-loader. Here's a simple example:
// index.html
<my-app foo="testing svelte"></my-app>
<script src="bundle.js"></script>
// script.js (bundled into bundle.js)
import App from './App.svelte';
customElements.define('my-app', App);
// App.svelte
<script>
export let foo;
console.log(foo); // undefined
$: bar = foo.toUpperCase(); // Cannot read property 'toUpperCase' of undefined
$: qux = String(foo).toUpperCase(); // No error, works
</script>
{ foo } // testing svelte - works, as expected
{ qux } // TESTING SVELTE - works, as expected
Also if customElement: true
is not set, and the framework is instantiated with const app = new App(...)
constructor, console.log(foo)
would work, just as $: bar = foo.toUpperCase()
.
Could anyone explain why Svelte works this way? Thanks, cheers!