rails 6 jquery not working in production 错误说 $ 没有定义

Posted

技术标签:

【中文标题】rails 6 jquery not working in production 错误说 $ 没有定义【英文标题】:rails 6 jquery not working in production error says $ isn't defined 【发布时间】:2021-07-11 09:04:59 【问题描述】:

我收到错误 $ is not defined only in production 错误不会发生在开发中,$ 是在 config/webpack/environment.js 中定义的

    const  environment  = require('@rails/webpacker')
    // const customConfig = require('./custom');
    const webpack = require('webpack')

    environment.plugins.prepend('Provide',
        new webpack.ProvidePlugin(
         $: "jquery",
        jQuery: "jquery",
        Popper: ['popper.js', 'default']
    ),)

    const aliasConfig = 
    'jquery': 'jquery-ui-dist/external/jquery/jquery.js',
    'jquery-ui': 'jquery-ui-dist/jquery-ui.js'
    ;

    environment.config.set('resolve.alias', aliasConfig);

    module.exports = environment

【问题讨论】:

【参考方案1】:

尝试添加到您的app/javascript/packs/application.js

global.$ = require("jquery")

【讨论】:

以上是关于rails 6 jquery not working in production 错误说 $ 没有定义的主要内容,如果未能解决你的问题,请参考以下文章

Jquery-ui Datepicker 未显示在 Rails 视图中,出现错误 Uncaught TypeError: $(...).datepicker is not a function

Rails 6.1.3,webpacler,select2,出现错误:readyException.js:6 Uncaught TypeError: $(...).select2 is not a f

Uncaught ReferenceError: Clipboard is not defined: when using clipboard.js with Rails 6

你如何让 JQuery 与 Rails 6 和 Webpacker 6 一起工作

物化 jQuery 在 Rails 6 中不起作用

rails 6 / webpacker tooltip and popover event listeners TypeError: $(...).tooltip is not a function