繁体   English   中英

如何将 Svelte 组件导入到 JS 文件中

[英]How to import a Svelte component into a JS file

我正在开发一个同时使用原生 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>

你可以简单地做

import MyComponent from 'MyComponent.svelte'

创建它的一个实例,然后你会做

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

如果您使用的是 JavaScript ES6,则导入 svelte 和其他任何内容的语法如下:

const svelte = require('svelte')

并且不要忘记通过简单地添加以下内容来更新rollup.config.js文件:

import svelte from "rollup-plugin-svelte";

svelte()

在插件的数组中是这样的:

plugin : [
svelte() 

]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM