不要使用相对导入.当您像这样固定所有控制器时:
pin_all_from "app/javascript/controllers", under: "controllers"
您可以生成以下import-maps
条:
<script type="importmap" data-turbo-track="reload">{
"imports": {
"controllers/application": "/assets/controllers/application-368d98631bccbf2349e0d4f8269afb3fe9625118341966de054759d96ea86c7e.js",
"controllers/hello_controller": "/assets/controllers/hello_controller-549135e8e7c683a538c3d6d517339ba470fcfb79d62f738a0a089ba41851a554.js",
"controllers": "/assets/controllers/index-ed00704a3bfd9aebb7c818233ee5411c3ce8050f411b77ec27c4252ded99a59c.js",
}
<!-- ^ this is what you can import -->
}</script>
application.js
是共享文件的一个示例,它在index.js
中导入:
import { application } from "controllers/application"
// ^
// NOTE: the name of the generated importmap is used to import
如果添加另一个文件,不要添加_controller.js
,因为它会在index.js
中自动加载(除非这是您想要的).
I have another answer explaining why relative paths don't work and how to maybe make them work:
https://stackoverflow.com/a/73136675/207090