如何在Rails 7中使用ImportMap安装JQuery?

发布于 2025-01-30 13:06:14 字数 1248 浏览 5 评论 0原文

我有一个新的Rails 7应用程序。我目前正在尝试从Rails 5开始学习所有新功能。我想在我的JavaScript文件中使用以下代码,但是到目前为止,我会收到以下错误:nuck fustrauct referacterror:$ bess nestion nswins nswind In定义。

$(document).on("turbo:load", () => {
  console.log("turbo!");
});

这是其他两个相关文件。如果我需要发布其他任何内容,请告诉我。

importmap.rb

pin "application", preload: true
pin "jquery", to: "https://ga.jspm.io/npm:[email protected]/dist/jquery.js", 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 "el-transition", to: "https://ga.jspm.io/npm:[email protected]/index.js"

pin_all_from "app/javascript/controllers", under: "controllers"

应用程序

import "@hotwired/turbo-rails"
import "jquery"

$(document).on("turbo:load", () => {
  console.log("turbo!");
});

I have a new Rails 7 application. I'm currently trying to learn all the new features since Rails 5. I want to use the following code in my javascript file, but so far I'm getting the following error: Uncaught ReferenceError: $ is not defined.

$(document).on("turbo:load", () => {
  console.log("turbo!");
});

Here are two other relevant files. If I need to post anything else please let me know.

importmap.rb

pin "application", preload: true
pin "jquery", to: "https://ga.jspm.io/npm:[email protected]/dist/jquery.js", 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 "el-transition", to: "https://ga.jspm.io/npm:[email protected]/index.js"

pin_all_from "app/javascript/controllers", under: "controllers"

application.js

import "@hotwired/turbo-rails"
import "jquery"

$(document).on("turbo:load", () => {
  console.log("turbo!");
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

≈。彩虹 2025-02-06 13:06:14

只需切换到< jspm jquery 将是全局的导入:

# config/importmap.rb

# NOTE: pin jquery to jsdelivr instead of jspm
pin "jquery", to: "https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.js"
// app/javascript/application.js

import "jquery"; // this import first
import "script"; // then your other imports that use `

只需切换到< jspm jquery 将是全局的导入:

# config/importmap.rb

# NOTE: pin jquery to jsdelivr instead of jspm
pin "jquery", to: "https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.js"
// NOTE: don't use relative imports: `import "./script"` // add `pin "script"` to `importmap.rb` console.log($); // ok
// app/javascript/script.js

console.log($)  // ok

一切都可以正常工作,一个导入,多个导入,jQuery插件。不需要额外的提升。

Just switch to CDN other than jspm, jQuery will be global on import:

# config/importmap.rb

# NOTE: pin jquery to jsdelivr instead of jspm
pin "jquery", to: "https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.js"
// app/javascript/application.js

import "jquery"; // this import first
import "script"; // then your other imports that use `

Just switch to CDN other than jspm, jQuery will be global on import:

# config/importmap.rb

# NOTE: pin jquery to jsdelivr instead of jspm
pin "jquery", to: "https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.js"
// NOTE: don't use relative imports: `import "./script"` // add `pin "script"` to `importmap.rb` console.log($); // ok
// app/javascript/script.js

console.log($)  // ok

Everything just works, one import, multiple imports, jquery plugins. No extra hoisting needed.

我为君王 2025-02-06 13:06:14

做其他答案说的事情将有效,但是当您固定新包装时,它将停止工作。

原因是所谓的JavaScript工作方式,这意味着它将重新排序您必须在块中重新组织导入的代码。这将打破jQuery窗口分配。

为避免这种情况,请

import jquery from "jquery"
window.jQuery = jquery
window.$ = jquery

在另一个文件中提取并将其调用单个导入。

就我而言,我有:

application.js

import "./src/jquery"

$(function(){
  console.log("Hey!")
})

和src/jquery.js带有上述代码。

Doing what the other answers say will work, but it will stop working when you pin a new package.

The reason is the so-called javascript hoisted way of working, which means that it will reorder the code you have to reorganize the imports in blocks. This will break jQuery window assignation.

To avoid that, extract

import jquery from "jquery"
window.jQuery = jquery
window.$ = jquery

in another file and call it in on single import.

In my case, I have:

application.js

import "./src/jquery"

$(function(){
  console.log("Hey!")
})

and src/jquery.js with above's code.

顾冷 2025-02-06 13:06:14

我需要在我的 application.js 文件中添加几行。

import "@hotwired/turbo-rails"
import jquery from "jquery"
window.jQuery = jquery
window.$ = jquery

I needed to add a few lines to my application.js file.

import "@hotwired/turbo-rails"
import jquery from "jquery"
window.jQuery = jquery
window.$ = jquery
蒲公英的约定 2025-02-06 13:06:14

我在这里尝试了所有其他建议,但这些建议都没有起作用。

这是您解决这个问题的方法:

  1. pin jquery
    bin/importMap Pin jQuery

  2. 使用jsdelivr.net或importmap.rb中的本地文件

    jspm由于某些未知原因而无法工作

    < br> a) pin“ jquery”,to:“ https://cdn.jsdelivr.net/npm/jquery/jquery/dist/jquery.js”

    br> b) pin“ jquery”,to:“ jquery.js”
    如果您使用本地文件,则需要下载jquery.js到app/javascript/jquery.js

    如果您想要该

    c) <代码> pin“ jQuery”,to:“ https://cdn.jsdelivr.net/npm/< href =” // cdn-cgi/l/l/email-protection“ class =” __ cf_email__“ data-cfemail > [email&nbsp; prected] /dist/jquery.js“

  3. 在application.js文件中,您只需要添加此

    导入“ jquery”

  4. 当您想在视图中使用jQuery时,您需要在脚本标签上使用type =“模块”

<script type="module">

 $(document).ready(function(){
   console.log($)
 })
</script>

I tried all of the other suggestions here and none of those worked.

Here is how you solve this:

  1. Pin jquery
    bin/importmap pin jquery

  2. Use jsdelivr.net or local file in importmap.rb
    jspm won't work for some unknown reason

    a) pin "jquery", to: "https://cdn.jsdelivr.net/npm/jquery/dist/jquery.js"

    b) pin "jquery", to: "jquery.js"
    if you use a local file, you need to download jquery.js to app/javascript/jquery.js

    You can also pin to a specific version if you want that
    c) pin "jquery", to: "https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.js"

  3. In application.js file you only need to add this
    import "jquery"

  4. When you want to use jquery in a view, you need use type="module" on the script tag

<script type="module">

 $(document).ready(function(){
   console.log($)
 })
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文