Rails 7 和导入映射以及加载自定义 JS 文件
我正在尝试加载我的自定义 js。但出现错误。
错误如下。
无法注册控制器:通知(controllers/notification_controller)错误:无法解析从 http://localhost:3000/assets/controllers/notification_contr...
│ ├── javascript
│ │ ├── application.js
│ │ ├── controllers
│ │ │ ├── application.js
│ │ │ ├── index.js
│ │ │ ├── notification_controller.js
│ │ └── lib
│ │ └── noty.js
notification_controller.js
import { Controller } from "@hotwired/stimulus"
import Noty from "@noty"
export default class extends Controller {
static targets = [ 'type', 'message' ]
....// some codes.
}
config/importmap.rb 导入的说明符“@noty”... notification_controller.js config/importmap.rb
# Pin npm packages by running ./bin/importmap
pin "application", preload: true
pin "@hotwired/turbo-rails", to: "turbo.min.js", preload: true
pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true
pin_all_from "app/javascript/controllers", under: "controllers"
## lib
pin "@noty", to: "app/javascript/lib/noty.js", preload: true
我做了什么做错了吗?我还有其他一些问题是,“stimulus.min.js”和“turbo.min.js”文件存在于哪里?
I am trying to load my custom js. but getting errors.
Error is following.
Failed to register controller: notification (controllers/notification_controller) Error: Unable to resolve specifier '@noty' imported from http://localhost:3000/assets/controllers/notification_contr...
│ ├── javascript
│ │ ├── application.js
│ │ ├── controllers
│ │ │ ├── application.js
│ │ │ ├── index.js
│ │ │ ├── notification_controller.js
│ │ └── lib
│ │ └── noty.js
notification_controller.js
import { Controller } from "@hotwired/stimulus"
import Noty from "@noty"
export default class extends Controller {
static targets = [ 'type', 'message' ]
....// some codes.
}
config/importmap.rb
# Pin npm packages by running ./bin/importmap
pin "application", preload: true
pin "@hotwired/turbo-rails", to: "turbo.min.js", preload: true
pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true
pin_all_from "app/javascript/controllers", under: "controllers"
## lib
pin "@noty", to: "app/javascript/lib/noty.js", preload: true
What did I do wrong? also some other questions I have is, where "stimulus.min.js" and "turbo.min.js" file exist?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的错误是因为您需要将路径
pin "@noty", to: "app/javascript/lib/noty.js", preload: true
修复为
pin "@noty", to: "lib/noty.js", preload: true
但是,由于库导出模块的方式,您在导入
Noty
时会遇到更多问题。添加库的最简单方法是通过资产管道。为此,
noty.js
添加到app/assets/config
noty.css
添加到app/assets/stylesheets
>//= link noty.js
添加到app/assets/config/manifest.js
app/views/layouts/application.html.erb ,
完成后
Noty
应该在您的 js 控制器中可用。例如Your error is because you need to fix the path
pin "@noty", to: "app/javascript/lib/noty.js", preload: true
to
pin "@noty", to: "lib/noty.js", preload: true
However, you are going to run into more problems importing
Noty
because of how the library exports modules. The easiest way to add the library is through the asset pipeline.To do this
noty.js
toapp/assets/config
noty.css
toapp/assets/stylesheets
//= link noty.js
toapp/assets/config/manifest.js
app/views/layouts/application.html.erb
Once this is done,
Noty
should be available in your js controllers. e.g.