Search code examples
embedsveltesvelte-component

Can a Svelte component be embedded in a non-Svelte app?


I work in a group that has several projects, and each one is written in a different framework. We would like to have some self-contained widgets whose behavior and appearance are standard, but can be used in any of the systems. I thought Svelte sounded like a good option, because it doesn't require adding a framework on the front end. But I can't find anything that says Svelte is usable within other systems; it has to be an all-Svelte app to have Svelte components.

Is that correct? Or is there some way to embed a Svelte component into another system?


Solution

  • You can include a Svelte component anywhere as soon as you have the reference to the DOM element you want to append your Svelte component.

    const container = document.querySelector('.container');
    
    //MyComponent is the compiled component
    new MyComponent({
      target : container
    });
    

    See the Svelte docs: https://svelte.dev/docs#run-time-client-side-component-api

    But then you have some limitation on how you can handle the state if you have multiple components included like that.