铁轨7:加载所有刺激控制器
我最近将我的应用程序从Rails 6升级到Rails 7,但是有些项目似乎随刺激控制器如何从javascript/Controllers
加载而变化。
I Rirs 6我能够从index.js
文件中的javascript/Controllers/Controllers
目录中的文件进行此操作:
const context = require.context("controllers", true, /_controller\.js$/)
application.load(definitionsFromContext(context))
但是在Rails 7中,此提升(在我的浏览器JS js台中):
Uncaught TypeError: __require.context is not a function
因此,我一直为我的每个刺激控制器称呼这个电话:
import FooBarController from "./foo_bar_controller"
application.register("foo_bar_controller", FooBarController)
进口和注册所有刺激控制器的正确方法是什么?我在文档中找不到有关此的任何细节。
更新:
我运行了刺激:安装耙子任务,它确实更改了我以前不正确的文件。但是现在,当我构建应用程序时,我会得到这个:
✘ [ERROR] Could not resolve "controllers/application"
app/javascript/controllers/index.js:3:28:
3 │ import { application } from "controllers/application"
╵ ~~~~~~~~~~~~~~~~~~~~~~~~~
You can mark the path "controllers/application" as external to exclude it from the bundle, which will remove this error.
✘ [ERROR] Could not resolve "@hotwired/stimulus-loading"
app/javascript/controllers/index.js:6:41:
6 │ import { eagerLoadControllersFrom } from "@hotwired/stimulus-loading"
╵ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You can mark the path "@hotwired/stimulus-loading" as external to exclude it from the bundle, which will remove this error.
这也是我在importmap.rb文件中所拥有的:
pin "application", preload: true
pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true
pin_all_from "app/javascript/controllers", under: "controllers"
I've recently upgraded my app from Rails 6 to Rails 7, but some items seem to have changed with how Stimulus controllers are loaded from javascript/controllers
.
I Rails 6 I was able to do this from an index.js
file in the javascript/controllers
directory:
const context = require.context("controllers", true, /_controller\.js$/)
application.load(definitionsFromContext(context))
However in Rails 7 this raises (in my browsers js console):
Uncaught TypeError: __require.context is not a function
So I'm stuck calling this for each of my Stimulus controllers:
import FooBarController from "./foo_bar_controller"
application.register("foo_bar_controller", FooBarController)
What is the right way to import and register all my Stimulus controllers in Rails 7? I cannot find any details on this in the docs.
UPDATE:
I ran the stimulus:install rake task, and it did change some of my files that I had that were previously incorrect. However now when I build the app I get this:
✘ [ERROR] Could not resolve "controllers/application"
app/javascript/controllers/index.js:3:28:
3 │ import { application } from "controllers/application"
╵ ~~~~~~~~~~~~~~~~~~~~~~~~~
You can mark the path "controllers/application" as external to exclude it from the bundle, which will remove this error.
✘ [ERROR] Could not resolve "@hotwired/stimulus-loading"
app/javascript/controllers/index.js:6:41:
6 │ import { eagerLoadControllersFrom } from "@hotwired/stimulus-loading"
╵ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You can mark the path "@hotwired/stimulus-loading" as external to exclude it from the bundle, which will remove this error.
This is what I have in my importmap.rb file as well:
pin "application", preload: true
pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true
pin_all_from "app/javascript/controllers", under: "controllers"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于您当前用作JavaScript Bundler/Builder的内容。
刺激手册 解释了导轨中的不同安装和自动加载控制器的不同方法。
require.context
仅通过WebPack提供。这已被Rails 7中的Hotwire+刺激取代(并且可选地 importmap )。听起来您当前在Esbuild上,因此您应该能够使用命令
rails刺激:清单:update
更新index.js
控制器导入。这可能需要您运行
导轨刺激:首先安装
。This depends on what you're currently using as a JavaScript bundler/builder.
The Stimulus Handbook explains the different methods of installation and autoloading controllers in Rails.
require.context
was only available through webpack. This has been replaced with Hotwire+Stimulus in Rails 7 (and optionally importmap).It sounds like you're currently on esbuild, so you should be able to update the
index.js
controller imports using the commandrails stimulus:manifest:update
.This may require that you run
rails stimulus:install
first.