javascript - How to import a Svelte component into a JS file - Stack Overflow

I'm working on a project using both of native JS and Svelte.And I worked on a Svelte ponent and I

I'm working on a project using both of native JS and Svelte. And I worked on a Svelte ponent and I want to know how can I import this later into my JS file.

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

**MyComponent.svelte**

<script>
   ... the Svelte code ...
</script>
<style>
   ... the Svelte ponent's style ...
</style>
<div>
   ...
   ...
</div>

I'm working on a project using both of native JS and Svelte. And I worked on a Svelte ponent and I want to know how can I import this later into my JS file.

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

**MyComponent.svelte**

<script>
   ... the Svelte code ...
</script>
<style>
   ... the Svelte ponent's style ...
</style>
<div>
   ...
   ...
</div>
Share asked Oct 19, 2020 at 15:52 Elishah LMTElishah LMT 231 silver badge3 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 6

You can simply do

import MyComponent from 'MyComponent.svelte'

to create an instance of it you would then do

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

If you are using JavaScript ES6 the syntax to import svelte and anything else is below:

const svelte = require('svelte')

And don't forget to update the rollup.config.js file by simply adding :

import svelte from "rollup-plugin-svelte";

and

svelte()

in the plugins' array like this :

plugin : [
svelte() 

]

With svelte 5.0, the way of mounting ponent into js has been changed. Now it looks like that:

import { mount } from "svelte";
mount(Component, {target: targetNode});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745061642a4609001.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信