Rails 6 webpack 抛出'Uncaught ReferenceError: $ is not defined' [英] Rails 6 webpack throwing 'Uncaught ReferenceError: $ is not defined'

查看:57
本文介绍了Rails 6 webpack 抛出'Uncaught ReferenceError: $ is not defined'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用 ruby​​ 2.6.5 开发 Rails 6.由于 rails 6 引入了 webpack,所以我正在尝试使用 webpack 加载我的 js 文件.虽然我已经在 appliation.js 中需要 jquery,但我仍然收到以下错误:

Hi i recently started working on Rails 6 with ruby 2.6.5. as the rails 6 has introduced the webpacks so i am trying to load my js files with the webpack. Although i have already required the jquery in the appliation.js still i am getting the below error:

'Uncaught ReferenceError: $ is not defined'

application.js

application.js

require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
require('packs/jquery')

我的 'jquery.js' 文件在 packs 目录中.如果我遗漏了什么,请告诉我.提前致谢:)

My 'jquery.js' file is inside the packs directory. Please let me know if i am missing something. Thanks in advance :)

推荐答案

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

const webpack = require('webpack')
environment.plugins.prepend('Provide', new webpack.ProvidePlugin({
  $: 'jquery/src/jquery',
  jQuery: 'jquery/src/jquery',
  jquery: 'jquery',
  'window.jQuery': 'jquery',
  Popper: ['popper.js', 'default']
}))

module.exports = environment

config/webpack/environment.js 中删除 require('packs/jquery')application.js

这篇关于Rails 6 webpack 抛出'Uncaught ReferenceError: $ is not defined'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆