如何在 Rails webpacker 3 中使用 jQuery

2024-04-06

我生成一个新的 Rails 应用程序: rails new titi --webpack=vue

并想使用 jQuery(或其他库,如 popper、vue-resource...)。

我尝试过了yarn add jquery这很好,但我无法在 JavaScript 代码中访问 jQuery。

在之前的 Webpacker gem 中,有更多的 conf,我必须将其写在shared.js :

module.exports = {
    ...
    plugins: [
      ...
      new webpack.ProvidePlugin({
          $: 'jquery',
          jQuery: 'jquery',
    ]
    ...
    resolve: {
      extensions: settings.extensions,
      modules: [
       resolve(settings.source_path),
        'node_modules'
      ],
      alias: {
        jquery: "jquery/src/jquery",
        vue: "vue/dist/vue.js",
        vue_resource: "vue-resource/dist/vue-resource",
    }
}

在当前 Webpacker 版本中包含库的最简洁方法是什么?东西在config/webpacker.yml?


更新为Webpacker 3.5.0 and [电子邮件受保护] /cdn-cgi/l/email-protection

Popper.js and [email protected] /cdn-cgi/l/email-protection不需要安装 JQuery。但是,Bootstrap 4 需要 JQuery 和 Popper.js,我认为这就是在原始示例中显示它们的目的。另外,我在示例中省略了 Vue,因为有足够的文档介绍如何添加 Vue 连接。

为了让一切正常工作,我安装了webpack-merge, popper.js, jquery, jquery-ujs, and [email protected] /cdn-cgi/l/email-protection通过纱线。

安装后,我可以更新./config/webpack/environment.js使用以下代码:

/*
  ./config/webpack/environment.js
  Info for this file can be found
  github.com/rails/webpacker/blob/master/docs/webpack.md
*/

const { environment } = require('@rails/webpacker')
const { merge } = require('webpack-merge')
const webpack = require('webpack')

// Add an additional plugin of your choosing : ProvidePlugin
environment.plugins.prepend('Provide', new webpack.ProvidePlugin({
    $: 'jquery',
    JQuery: 'jquery',
    jquery: 'jquery',
    'window.Tether': "tether",
    Popper: ['popper.js', 'default'], // for Bootstrap 4
  })
)

const envConfig = module.exports = environment
const aliasConfig = module.exports = {
  resolve: {
    alias: {
      jquery: 'jquery/src/jquery'
    }
  }
}

module.exports = merge(envConfig.toWebpackConfig(), aliasConfig)

现在envConfig.toWebpackConfig()在最后一个语句中使用environment.js,我需要改变development.js, production.js, test.js至以下内容:

const environment = require('./environment')

module.exports = environment

Then in ./app/javascript/application.js我添加了以下内容:

// ./app/javascript/application.js

/* eslint no-console:0 */
// This file is automatically compiled by Webpack, along with any other files
// present in this directory. You're encouraged to place your actual application logic in
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.
//
// To reference this file, add <%= javascript_pack_tag 'application' %> to the appropriate
// layout file, like app/views/layouts/application.html.erb

console.log('Hello World from Webpacker')

// JS libraries
import "jquery"
import "jquery-ujs"
import "bootstrap"

为了确保 JQuery 没有通过资产管道加载,我删除了 Rails 资产连接./app/assets/javascripts/application.js:

// require rails-ujs
// require_tree .

然后我将这两行添加到header部分在./app/views/layouts/application.html.haml显示Webpack内容:

= stylesheet_pack_tag 'application'
= javascript_pack_tag 'application' 

制作完成后static_pages控制器和static_pages#index查看,启动 Rails 服务器后(rails s),我能够进入浏览器的 JS 控制台并运行console.log(jQuery().jquery);查看 JQuery 是否正在加载。一切都按预期加载。

文档可以在这里找到:https://github.com/rails/webpacker/blob/master/docs/webpack.md https://github.com/rails/webpacker/blob/master/docs/webpack.md

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Rails webpacker 3 中使用 jQuery 的相关文章

随机推荐