How to expose a function in svelte that can accept parameters to render?

那年仲夏 提交于 2021-02-10 05:28:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!