Skip to content

Instantly share code, notes, and snippets.

@ayaysir
Created November 30, 2020 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayaysir/321b0c3b486a6d65f62e12c977c8d2da to your computer and use it in GitHub Desktop.
Save ayaysir/321b0c3b486a6d65f62e12c977c8d2da to your computer and use it in GitHub Desktop.
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