简体   繁体   English

如何将 StimulusJS 安装到 Ruby on Rails 应用程序?

[英]How do I install StimulusJS to a Ruby on Rails application?

How do I install StimulusJS to a Ruby on Rails application?如何将 StimulusJS 安装到 Ruby on Rails 应用程序?

The docs mention webpack and npm , but nothing about Rails or Yarn (at time of this writing).文档提到了webpacknpm ,但没有提到 Rails 或 Yarn(在撰写本文时)。

(Kind of strange given that Stimulus comes from Basecamp, who literally created RoR...) (有点奇怪,因为 Stimulus 来自 Basecamp,他确实创造了 RoR ......)

The answer, at least for Rails 6, is stunningly easy:至少对于 Rails 6,答案非常简单:

bundle exec rails webpacker:install:stimulus

That's it .就是这样 That command will:该命令将:

  • invoke yarn to download and install StimulusJS调用yarn下载安装StimulusJS
  • add a line to app/javascript/packs/application.js to import javascript controllersapp/javascript/packs/application.js添加一行以导入 javascript 控制器
  • add file app/javascript/controllers/index.js (to load/init all stimulus controllers)添加文件app/javascript/controllers/index.js (加载/初始化所有刺激控制器)
  • add a demo controller in app/javascript/controllers/hello_controller.jsapp/javascript/controllers/hello_controller.js添加演示控制器

From there, you can create a simple view to use the demo Hello controller, and with a little tweaking you can make it just like the demo on the StimulusJS.org home page.从那里,您可以创建一个简单的视图来使用演示 Hello 控制器,通过一些调整,您可以使它就像StimulusJS.org主页上的演示一样。

Webpacker instructions for Stimulus are here: https://github.com/rails/webpacker#stimulus Stimulus 的 Webpacker 说明在这里: https : //github.com/rails/webpacker#stimulus

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM