如何将 Svelte 组件导入 JS 文件

Eli*_*LMT 2 javascript rollup svelte

我正在开发一个使用原生 JS 和 Svelte 的项目。我开发了一个 Svelte 组件,我想知道稍后如何将其导入到我的 JS 文件中。

*index.js*
// js code here 
alert('here is my sweet Svelte compoent');
-- the svelte component must be here ---
...
//

**MyComponent.svelte**

<script>
   ... the Svelte code ...
</script>
<style>
   ... the Svelte component's style ...
</style>
<div>
   ...
   ...
</div>
Run Code Online (Sandbox Code Playgroud)

Ste*_*aes 6

你可以简单地做

import MyComponent from 'MyComponent.svelte'
Run Code Online (Sandbox Code Playgroud)

然后创建它的实例

new MyComponent({
  target: mountpoint // here the dom node where you want to mount it
})
Run Code Online (Sandbox Code Playgroud)