Rails 7 和导入映射以及加载自定义 JS 文件

发布于 2025-01-10 09:54:58 字数 1428 浏览 3 评论 0原文

我正在尝试加载我的自定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筱武穆 2025-01-17 09:54:58

您的错误是因为您需要将路径

pin "@noty", to: "app/javascript/lib/noty.js", preload: true 修复

pin "@noty", to: "lib/noty.js", preload: true

但是,由于库导出模块的方式,您在导入 Noty 时会遇到更多问题。添加库的最简单方法是通过资产管道。

为此,

  1. noty.js 添加到 app/assets/config
  2. noty.css 添加到 app/assets/stylesheets >
  3. //= link noty.js 添加到 app/assets/config/manifest.js
  4. 将其添加到 app/views/layouts/application.html.erb ,
<%= stylesheet_link_tag "noty" %>
<%= javascript_include_tag "noty" %>

完成后 Noty 应该在您的 js 控制器中可用。例如

import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static targets = [ 'type', 'message' ]

  test(){
    var noty = new Noty({text: 'Hi!'});
    noty.show();
  }
}

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

  1. Add noty.js to app/assets/config
  2. Add noty.css to app/assets/stylesheets
  3. Add //= link noty.js to app/assets/config/manifest.js
  4. Add this to app/views/layouts/application.html.erb
<%= stylesheet_link_tag "noty" %>
<%= javascript_include_tag "noty" %>

Once this is done, Noty should be available in your js controllers. e.g.

import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static targets = [ 'type', 'message' ]

  test(){
    var noty = new Noty({text: 'Hi!'});
    noty.show();
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文