Passing variable to Custom Svelte Web Component

Viewed 1749

I have created simple Custom Web Component using Svelte. It has been compiled and seems it should work well, but there is the difficulty. I'm trying to pass into prop some variable, but getting undefined all the time, but if I'm passing some string

Result.svelte component

<svelte:options tag="svelte-result" />

<script>
    export let result = {metadata: {}, transfers: []};
    export let string = 'no string';
</script>

<div class="result__wrapper">
    {string}
    <div class="result__metadata">
        <div>{result.metadata.offset}</div>
        <div>{result.metadata.limit}</div>
        <div>{result.metadata.total}</div>
    </div>
</div>

When it copiled I'm using it like

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Svelte test</title>
  <script defer src="/svelte/wapi-client/svelte-component.js"></script>
  
</head>
<body>
  <div id="test"></div>
</body>

<script>
  const data = {
    metadata: {
      limit: 20,
      offset: 0,
      total: 311301
    },
    transfers: [
      {
        amount: "7.95",
        identifier: "9cd9901f-44a5-4436-9aef-880354bbe2e4"
      }
    ]
  };

  document.getElementById('test').innerHTML = `
    <svelte-result string="works" result=${data}></svelte-result>`;
</script>
</html>

data variable not passed to component, but string passed and shown correctly... What Am I doing wrong? How can I pass data variable into component ?

2 Answers

You can't pass objects as attributes to custom elements. You need to stringify your object before passing it.

index.html

...
document.getElementById('test').innerHTML = `
    <svelte-result string="works" result=${JSON.stringify(data)}></svelte-result>`;
...

Foo.svelte

<svelte:options tag="svelte-result" />

<script>
    export let result = {metadata: {}, transfers: []};
    export let string = 'no string';
        
    $: _result = typeof result === 'string' ? JSON.parse(result) : result;
</script>

<div class="result__wrapper">
    {string}
    <div class="result__metadata">
        <div>{_result.metadata.offset}</div>
        <div>{_result.metadata.limit}</div>
        <div>{_result.metadata.total}</div>
    </div>
</div>

As an alternative to using JSON.stringify to pass the data to the component, you can pass it as a property rather than as an attribute — in other words instead of this...

document.getElementById('test').innerHTML = `
  <svelte-result string="works" result=${data}></svelte-result>`;

...you do this:

document.getElementById('test').innerHTML = `
  <svelte-result string="works"></svelte-result>`;

document.querySelector('svelte-result').result = data;

Not ideal, of course, since it means that you have to accommodate the initial undefined state and the post-initialisation state once result has been passed through, but web components are a bit awkward like that.

Related