问题
I'm pretty new to svelte, and for my use case I would like to export the svelte app as a bundle.js
which exposes a function let's say startApp(positionInject, appConfiguration)
that can accept 2 parameters (positionInject
is the app injection position eg: .app, appConfiguration
is the initial configuration of the app to start), based on those parameters svelte app start renders.
I am wondering, this is possible in svelte?
Appreciate any help.
回答1:
Every Svelte component takes a target
element and props
as constructor parameters. You can wrap the construction in a function:
import App from './App.svelte';
export function startApp(selector, props) {
const target = document.querySelector(selector)
return new App({
target,
props
})
}
And you can call it like this:
import {startApp} from "./bundle.js"
startApp(".my-app", {config: ...})
来源:https://stackoverflow.com/questions/59228891/how-to-expose-a-function-in-svelte-that-can-accept-parameters-to-render