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

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

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

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

i see the following error

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

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

import { Application } from "@hotwired/stimulus"
import NestedForm from 'stimulus-rails-nested-form'

const application = Application.start()
// application.register('nested-form', NestedForm)
// Configure Stimulus development experience
application.debug = false
window.Stimulus   = application

export { application }


below are the contents of my package.json

{
  "name": "backend",
  "private": true,
  "dependencies": {
    "@hotwired/stimulus": "^3.2.1",
    "@hotwired/stimulus-webpack-helpers": "^1.0.1",
    "@rails/webpacker": "5.4.4",
    "node": "^16.20.1",
    "stimulus-rails-nested-form": "^4.1.0",
    "webpack": "^4.46.0",
    "webpack-cli": "^3.3.12"
  },
  "devDependencies": {
    "@babel/plugin-proposal-private-methods": "^7.18.6",
    "@babel/plugin-proposal-private-property-in-object": "^7.21.11",
    "webpack-dev-server": "^3"
  }
}

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这里使用了命名导入,如下所示:

import { Controller as n } from "@hotwired/stimulus";

class r extends n { ... }

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

import Stimulus from "@hotwired/stimulus";

class r extends Stimulus.Controller { ... }

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

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

import { Controller as n } from "@hotwired/stimulus"

class r extends n { ... }

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

import Stimulus from "@hotwired/stimulus"

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.

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:

确定