Rails 7: Stimulus Rails嵌套表单不起作用

huangapple go评论107阅读模式
英文:

Rails 7: Stimulus Rails Nested Form not working

问题

抱歉,您提供的内容似乎包含了一些代码和错误信息,但我无法提供针对具体代码和错误的翻译,因为这需要更多的上下文和代码理解。如果您有关于代码和错误的具体问题,可以提出并提供更多信息,我将尽力提供帮助。

英文:

I am trying to follow the instrcutions mentioned here

https://www.stimulus-components.com/docs/stimulus-rails-nested-form/

but when I add the import statement

  1. import NestedForm from 'stimulus-rails-nested-form'

i see the following error

  1. ERROR in ./node_modules/stimulus-rails-nested-form/dist/stimulus-rails-nested-form.mjs 2:16-17
  2. Can't import the named export 'Controller' from non EcmaScript module (only default export is available)

below is my app/javascript/controllers/application.js

  1. import { Application } from "@hotwired/stimulus"
  2. import NestedForm from 'stimulus-rails-nested-form'
  3. const application = Application.start()
  4. // application.register('nested-form', NestedForm)
  5. // Configure Stimulus development experience
  6. application.debug = false
  7. window.Stimulus = application
  8. export { application }

below are the contents of my package.json

  1. {
  2. "name": "backend",
  3. "private": true,
  4. "dependencies": {
  5. "@hotwired/stimulus": "^3.2.1",
  6. "@hotwired/stimulus-webpack-helpers": "^1.0.1",
  7. "@rails/webpacker": "5.4.4",
  8. "node": "^16.20.1",
  9. "stimulus-rails-nested-form": "^4.1.0",
  10. "webpack": "^4.46.0",
  11. "webpack-cli": "^3.3.12"
  12. },
  13. "devDependencies": {
  14. "@babel/plugin-proposal-private-methods": "^7.18.6",
  15. "@babel/plugin-proposal-private-property-in-object": "^7.21.11",
  16. "webpack-dev-server": "^3"
  17. }
  18. }

I am on rails Rails 7.0.3. Any help on what could be missing would be great, Thanks.

答案1

得分: 1

我的JavaScript技能不够好,但似乎这是stimulus-rails-nested-form中的一个bug。它的.mjs文件,即默认的模块入口点,以错误的方式导入了*@hotwired/stimulus*。正如错误提到的,该模块仅支持默认导出,但却进行了命名导入。

stimulus-rails-nested-form这里使用了命名导入,如下所示:

  1. import { Controller as n } from "@hotwired/stimulus";
  2. class r extends n { ... }

正如错误提到的,导入的模块只有默认导出,所以应该像这样导入:

  1. import Stimulus from "@hotwired/stimulus";
  2. class r extends Stimulus.Controller { ... }

由于这需要更改库,因此在那之前,可以通过从stimulus-rails-nested-form提供的UMD文件进行导入来解决问题。

  1. import NestedForm from 'stimulus-rails-nested-form/dist/stimulus-rails-nested-form.umd.js';
英文:

My Javascript fu, is not good enougth, but it seems this is a bug in stimulus-rails-nested-form. Its '.mjs' file, the default module entrypoint, imports @hotwired/stimulus in an incorrect way. As the error mentions only default export is available in the module, but a named import it been done.

stimulus-rails-nested-form uses a named import here, like this

  1. import { Controller as n } from "@hotwired/stimulus"
  2. class r extends n { ... }

As the error mentions, the imported module only has a default export, so it should be imported like this:

  1. import Stimulus from "@hotwired/stimulus"
  2. class r extends Stimulus.Controller { ... }

Since this requires a library change, until then, it can be solved by importing from the UMD file that stimulus-rails-nested-form provides.

  1. import NestedForm from 'stimulus-rails-nested-form/dist/stimulus-rails-nested-form.umd.js'

huangapple
  • 本文由 发表于 2023年7月13日 20:04:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679177.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定