WARNING
This feature is currently only supported in Svelte 4 and svelte-standalone@latest
.
Leveraging Component API
Svelte Standalone exposes the $set
and $on
methods from the Svelte Component API at window.<componentId>
. These methods allow you to interact with your standalone components programmatically.
Available Methods
$set(props)
Updates the component's props dynamically. This method is useful for modifying the component's state after it has been mounted.
Example:
window.myComponent.$set({
title: 'Updated Title',
description: 'This is an updated description.'
});
$on(event, callback)
Listens for custom events emitted by the component. This method allows you to handle events such as user interactions or state changes.
Example:
window.myComponent.$on('click', (event) => {
console.log('Component clicked!', event.detail);
});
Example Usage
Here’s a complete example of how to use these methods with a standalone component:
Create a Component:
bashnpx standalone create
Name your component (e.g.,
myComponent
) and choose an embedding strategy.Build the Component:
bashnpx standalone build
Include the Component in Your HTML:
html<script src="/path/to/myComponent.min.js"></script>
Interact with the Component:
javascript// Start the component window.myComponent.start({ title: 'Initial Title', description: 'This is the initial description.' }); // Update the component's props window.myComponent.$set({ title: 'Updated Title', description: 'This is an updated description.' }); // Listen for a custom event window.myComponent.$on('click', (event) => { console.log('Component clicked!', event.detail); });