Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

327
Views
Is there any way how to pass component/html into string in Svelte?

What I am trying to achieve is to pass component into a string of a variable or some similar solution. I have tried {@html someVariable} but it works for me only in one way it means text from string to HTML. But I need the text from HTML to string. I've tried document.elementById() but always get return 'document is not defined'. Here is an example of what I'm trying to achieve:

App.svelte

<script>
import Component from './component.svelte';
import Description  from '.description .svelte';


// How to declare component/html into variable?

let test = 'something like <Component /> but acceptable by variable'
let lala = test;

</script>

<Description {test} />  
Description.svelte

<script>
export const lala
</script>

{#if something}
{@lala html}
{:else if something }
nope
{/if}

I think that this question Rendering Svelte components from HTML string has kind of the answer but I failed to make it functional.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Im not sure what you mean by "text from HTML to string", but if you want to pass a component to a child component you can pass it as a prop:

<!-- App.svelte -->
<script>
    import Component from './Component.svelte';
    import Description  from './Description.svelte';
</script>

<Description component={Component} />  

To display a component that is defined in a variable you can use the <svelte:component> special element.

<!-- Description.svelte -->
<script>
    export let component;
</script>

{#if component}
    <svelte:component this={component}></svelte:component>
{:else}
    component isn't set
{/if}

Here is a working Svelte REPL Link.

If you want to learn more about the <svelte:component> special element check out the tutorial.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!