簡體   English   中英

Rails 6 Stimulus.js 無法運行

[英]Rails 6 Stimulus.js won't run

我在我的新 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不會在我的開發控制台中呈現,也不會看到任何錯誤消息。

不完全確定我的設置有什么問題(默認設置),但是,這個 repo 設置有效! https://github.com/rodloboz/stimulus-rails

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM