2

I'm working on a project using both of native JS and Svelte. And I worked on a Svelte component 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 compoent');
-- the svelte component must be here ---
...
//

**MyComponent.svelte**

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

4 Answers 4

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
})
Sign up to request clarification or add additional context in comments.

Comments

1

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

const svelte = require('svelte')

1 Comment

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

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() 

]

Comments

0

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

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

1 Comment

Sure, you need to import component too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.