我尝试加载自定义js,但遇到了错误。
错误消息如下。
未能注册控制器:notification (controllers/notification_controller) 错误:无法解析从 http://localhost:3000/assets/controllers/notification_controller... 导入的 '@noty' 模块。
│ ├── 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
我做错了什么?另外还有一些问题,"stimulus.min.js"和"turbo.min.js"文件在哪里存在?
pin "@noty", to: "lib/noty.js", preload: true
对我不起作用。浏览器一直提示“相对引用必须以“/”、“./”或“../”开头”。”。 - Robert Reiz