Rails 6 Stimulus.js无法运行

问题描述

我在新的Rails 6应用程序中安装了Stimulus.js。

在我的packs/application.js文件中,我具有以下内容

// This file is automatically compiled by Webpack,along with any other files
// present in this directory. You're encouraged to place your actual application logic in
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.

require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")


// Uncomment to copy all static images under ../images to the output folder and reference
// them with the image_pack_tag helper in views (e.g <%= image_pack_tag 'rails.png' %>)
// or the `imagePath` JavaScript helper below.
//
// const images = require.context('../images',true)
// const imagePath = (name) => images(name,true)

import "controllers";

app/javascript/controllers/index.js内,我有以下内容

// Load all the controllers within this directory and all subdirectories. 
// Controller files must be named *_controller.js.

import { Application } from "stimulus"
import { deFinitionsFromContext } from "stimulus/webpack-helpers"

const application = Application.start()
const context = require.context(".",true,/.js$/)
application.load(deFinitionsFromContext(context))

我有一个hello_controller.js,看起来像

// Visit The Stimulus Handbook for more details 
// https://stimulusjs.org/handbook/introduction
// 
// This example controller works with specially annotated HTML like:
//
// <div data-controller="hello">
//   <h1 data-target="hello.output"></h1>
// </div>

import { Controller } from "stimulus"

export default class extends Controller {
  connect() {
    console.log('HELLO');
  }
}

我已经将其插入以用于DOM元素,如下所示:

<div data-controller="hello">Hello,world</div>

当webpacker打包所有内容并刷新站点时,我的console.log不在我的开发控制台中呈现,也没有看到任何错误消息。

解决方法

不能完全确定我的设置有什么问题(默认设置),但是此回购设置有效! https://github.com/rodloboz/stimulus-rails

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...