Created
April 29, 2019 04:09
-
-
Save colinbate/3f56197c41b2b0fc3f4e700cf7121528 to your computer and use it in GitHub Desktop.
Svelte 3 custom element events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Test Component</title> | |
</head> | |
<body> | |
<script src="svelte.js"></script> | |
<sample-comp id="svelte"></sample-comp> | |
<script> | |
const comp1 = document.getElementById('svelte'); | |
// This one does not work. | |
//comp1.addEventListener('save', function (ev) { | |
// This one does. | |
comp1.$on('save', function (ev) { | |
console.log(ev.detail); | |
}); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "svelte3-component", | |
"version": "1.0.0", | |
"scripts": { | |
"build": "rollup -c" | |
}, | |
"dependencies": { | |
"svelte": "^3.1.0" | |
}, | |
"devDependencies": { | |
"rollup": "^1.10.1", | |
"rollup-plugin-node-resolve": "^4.2.3", | |
"rollup-plugin-svelte": "^5.0.3" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import svelte from 'rollup-plugin-svelte'; | |
import resolve from 'rollup-plugin-node-resolve'; | |
export default { | |
input: 'Sample.svelte', | |
output: { | |
file: 'svelte.js', | |
name: 'sample', | |
format: 'iife', | |
}, | |
plugins: [ | |
svelte({ | |
customElement: true, | |
emitCss: false, | |
css: false, | |
}), | |
resolve(), | |
], | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
import { createEventDispatcher } from 'svelte'; | |
let firstName = ''; | |
const dispatch = createEventDispatcher(); | |
function save() { | |
dispatch('save', { | |
firstName | |
}); | |
} | |
</script> | |
<svelte:options tag="sample-comp"/> | |
<div> | |
<h1>Hello World</h1> | |
<input type="text" bind:value={firstName}> | |
<button type="button" on:click={save}>Send</button> | |
</div> | |
<style> | |
div { | |
font-family: Georgia, 'Times New Roman', Times, serif; | |
} | |
</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment