Rails 7 接受了 Import maps 的使用,有了這個新技術,載入資源變得輕鬆多了。
如果您想知道如何在純 HTML 中使用 importmap,這裡有一個示範:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Import maps without Rails - Local-time example</title>
<script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
<script type="importmap-shim">
{
"imports": {
"local-time": "https://ga.jspm.io/npm:[email protected]/app/assets/javascripts/local-time.js"
}
}
</script>
<script type="module-shim">
import LocalTime from "local-time"
LocalTime.start()
</script>
<style>
time { color: #c11; font-size: 1.1em; }
</style>
</head>
<body>
<h1>Import maps without Rails - Local-time JS example</h1>
<p>
Last time I had chocolate was <time datetime="2022-05-08T23:00:00+02:00" data-local="time-ago">8th of May</time>
</p>
</body>
</html>
這個示範使用 importmap 載入 local-time js,將 HTML 元素從 UTC 轉換為瀏覽器的本地時間(更多資訊)。
© Copyrights 從想像到創造. All Rights Reserved.