$ is not defined when installing jQuery in Rails via Webpack

后端 未结 5 1424
猫巷女王i
猫巷女王i 2021-02-18 19:04

I\'m trying to install jQuery in Rails 6.0.0.rc1 via Webpack and I\'m not sure what I\'m missing but I\'m getting the error $ is not defined in the browser console

5条回答
  •  甜味超标
    2021-02-18 19:24

    The code in config/webpack/environment.js should look like this:

    environment.plugins.prepend('Provide',
      new webpack.ProvidePlugin({
        $: 'jquery/src/jquery',
        jQuery: 'jquery/src/jquery'
      })
    )
    

    see also https://www.botreetechnologies.com/blog/introducing-jquery-in-rails-6-using-webpacker

提交回复
热议问题