Skip to content

Instantly share code, notes, and snippets.

@ayaysir

ayaysir/add.js

Created Nov 30, 2020
Embed
What would you like to do?
dynamic module import example
let currentIdx = 1
export function add() {
const $ul = document.getElementById("todo-list")
const $li = document.createElement("li")
$li.textContent = `할 일 ${currentIdx++}`
$ul.appendChild($li)
}
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<button id="add-todo">할 일 추가</button>
<ul id="todo-list"></ul>
<script src="main.js" type="module"></script>
</body>
</html>
// import add from './add.js'
const onClick = e => {
import('./add.js').then(obj => {
obj.add()
})
}
document.getElementById("add-todo").addEventListener("click", onClick)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment