Web Components in Svelte
Video
Demos
HTML
Angular
React
Vue
How to create a web component with Svelte
1. Go to the rollup.config.js file in the root of the project.
2. Change the output.file to to the name of the webcomponent you want to export.
1 2 3 4 5 6 |
|
3. Change change compiler options to this.
1 2 3 4 5 6 7 |
|
4. Delete the css plugin below the svelte component.
5. Go to main.js and delete all config in your app component.
1 2 3 4 5 |
|
6. Create the component you want to export in your App.svelte file. You will need to include the svelte:option component with tag prop equaling the name of your component.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
7. Go you your index.html page and delete the css imports.
8. Include your new component in a defered script tag.
1 |
|
9. Use your component in your html
1 |
|
10. Run npm run build
11. Run npm start to see your component.
Note
Everytime you want to see a change you will have to run npm run build. There is probably a way you can watch the file.
How to use svelte component in Angular
1. Create a folder in your src called js and copy the web component.js files there.
2. Go to your angular.json file and include the javascript. It's will be under project -> architect -> build -> scripts.
1 2 3 |
|
3. Go to src -> app -> app.module.ts and add a custom schema to the ng module.
1 2 3 4 5 6 7 8 9 10 |
|
3. Go to your app.compoenent.html file and insert your web component.
1 |
|
How to use svelte component in React
1. Copy the component js into the public folder.
2. In the index.html file on the public folder include the js you copied with a defer attribute.
1 |
|
3. Use the webcomponent in your App component.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
How to use svelte component in Vue
1. Copy the component js into the public folder.
2. In the index.html file on the public folder include the js you copied with a defer attribute.
1 |
|
3. Use the component in any of the .vue component files.
1 2 3 4 5 6 |
|